Question 01

Write the following queries in SQL, using the university schema.
a. Find the ID and name of each student who has taken at least one Comp. Sci. course;
make sure there are no duplicate names in the result.

  1. -- a. Find the ID and name of each student who has taken at least
  2. -- one Comp. Sci. course
  3. -- make sure there are no duplicate names in the result
  4. select * from student limit 3;
  5. select * from takes limit 3;
  6. select * from course limit 3;
  7. select distinct ID, name
  8. from student natural join takes natural join course
  9. where course.dept_name = 'Comp. Sci.'

b. Find the ID and name of each student who has not taken any course offered before 2017.

  1. -- b. Find the ID and name of each student
  2. -- who has not taken any course offered before 2017.
  3. select * from student limit 5;
  4. select * from takes limit 7;
  5. select *
  6. from student natural join takes
  7. limit 7;
  8. select ID, name
  9. from student
  10. except
  11. select ID, name
  12. from student natural join takes
  13. where year < 2017

c. For each department, find the maximum salary of instructors in that department.
You may assume that every department has at least one instructor.

  1. -- c. For each department, find the maximum salary of instructors in that department.
  2. -- You may assume that every department has at least one instructor.
  3. select * from instructor limit 3;
  4. select max(salary) as MAXIMUM, name, ID,dept_name
  5. from instructor
  6. group by dept_name
  7. order by MAXIMUM

d. Find the lowest, across all departments, of the per-department maximum salary computed by the preceding query.

  1. -- d. Find the lowest, across all departments,
  2. -- of the per-department maximum salary computed by the preceding query.
  3. select dept_name, max(salary) as MAXIMUM
  4. from instructor
  5. group by dept_name
  6. order by MAXIMUM asc;
  7. select MIN(MAXIMUM),dept_name,name
  8. from (
  9. select dept_name, max(salary) as MAXIMUM, name
  10. from instructor
  11. group by dept_name
  12. order by MAXIMUM asc);

Question 02

Write the SQL statements using the university schema to perform the following operations:
a. Create a new course “CS-001”, titled “Weekly Seminar”, with 0 credits.

  1. -- a. Create a new course CS-001”, titled Weekly Seminar”, with 0 credits.
  2. select * from course limit 3;
  3. insert into course(course_id, title,dept_name,credits)
  4. values ('CS-001', 'Weekly Seminar', 'Comp. Sci.', 0);
  5. select *
  6. from course

b. Create a section of this course in Fall 2017, with sec id of 1,
and with the location of this section not yet specified.

  1. -- b. Create a section of this course in Fall 2017, with sec id of 1,
  2. -- and with the location of this section not yet specified.
  3. select * from section limit 3;
  4. insert into section(course_id, sec_id, semester, year, building, room_number)
  5. values ('CS-001','1','Fall',2009,'E4','1001');
  6. select * from section;

c. Enroll every student in the Comp. Sci. department in the above section.

  1. -- c. Enroll every student in the Comp. Sci. department in the above section.
  2. insert into takes(ID, course_id, sec_id, semester, year)
  3. select ID,'CIS-001','1','Fall',2017
  4. from student
  5. where dept_name = 'Comp. Sci.';

d. Delete enrollments in the above section where the student’s ID is 12345.

  1. -- d. Delete enrollments in the above section where the students ID is 12345.
  2. select * from takes;
  3. delete from takes
  4. where (course_id = 'CIS-001') and (sec_id = '1') and (semester = 'Fall') and (year = 2017)
  5. and (ID in (
  6. select ID
  7. from student
  8. where ID = 12345
  9. )
  10. );
  11. select * from takes

e. Delete the course CS-001. What will happen if you run this delete statement without first deleting offerings (sections) of this course?

  1. /*
  2. e. Delete the course CS-001. What will happen
  3. -- if you run this delete statement without
  4. -- first deleting offerings (sections) of this course?
  5. */
  6. delete from course where course_id = 'CS-001';
  7. select * from section;
  8. select * from course;
  9. select name, sql
  10. from sqlite_master
  11. where name = 'section'
  12. /*
  13. Comment - no any error. After delete the course,
  14. `section` has foreign key, course_id, but that foreign key has
  15. 'ON DELETE cascade' constraints.
  16. When deleting CS-001 from course which is referenced by `section`,
  17. tuples in `section` that have course_id as 'CS-001' will be automatically deleted.
  18. */

f. Delete all takes tuples corresponding to any section of any course with the word “advanced” as a part of the title; ignore case when matching the word with the title.

  1. /*
  2. f. Delete all takes tuples corresponding to any section of
  3. any course with the word “advanced” as a part of the title;
  4. ignore case when matching the word with the title.
  5. */
  6. select * from course where title like '%Intro. to%';
  7. select *
  8. from takes natural join course
  9. where title like '%tics%';
  10. delete from takes
  11. where course_id in (
  12. select course_id
  13. from course
  14. where title like '%advanced'
  15. );