book cover
Database System Concepts
Seventh Edition
Avi Silberschatz
Henry F. Korth
S. Sudarshan


line separator

Intermediate SQL

line separator

Using the university schema, write the following queries. In some cases you need to insert extra data to show the effect of a particular feature -- this is indicated with the question. You should then show not only the query, but also the insert statements to add the required extra data.

  1. Find the maximum and minimum enrollment across all sections, considering only sections that had some enrollment, don't worry about those that had no students taking that section
  2. Find all sections that had the maximum enrollment (along with the enrollment), using a subquery.
  3. As in in Q1, but now also include sections with no students taking them; the enrollment for such sections should be treated as 0. Do this in two different ways (and create require data for testing)
    1. Using a scalar subquery
    2. Using aggregation on a left outer join (use the SQL natural left outer join syntax)
  4. Find all courses whose identifier starts with the string "CS-1"
  5. Find instructors who have taught all the above courses
    1. Using the "not exists ... except ..." structure
    2. Using matching of counts which we covered in class (don't forget the distinct clause!).
  6. Insert each instructor as a student, with tot_creds = 0, in the same department
  7. Now delete all the newly added "students" above (note: already existing students who happened to have tot_creds = 0 should not get deleted)
  8. Some of you may have noticed that the tot_creds value for students did not match the credits from courses they have taken. Write and execute query to update tot_creds based on the credits passed, to bring the database back to consistency. (This query is provided in the book/slides.)
  9. Update the salary of each instructor to 10000 times the number of course sections they have taught.
  10. Create your own query: define what you want to do in English, then write the query in SQL. Make it as difficult as you wish, the harder the better.

line separator