2 Database schema for the exercises Professor(ssn, profname, status, salary)Course(crscode, crsname, credits)Taught(crscode, semester, ssn)Assumption: (1) Each course has only one instructor in each semester; (2) all professors have different salaries; (3) all professors have different names; (4) all courses have different names; (5) status can take values from “Full”, “Associate”, and “Assistant”.
3 Query 1Return those professors who have taught ‘csc6710’ but never ‘csc7710’.
17 SQL Solution (SELECT ssn FROM Taught T WHERE T.crscode=‘CSC6710’ OR T.crscode=‘CSC7710’)Except(SELECT T1.ssnFrom Taught T1, Taught T2,Where T1.crscode = ‘CSC6710’) AND T2.crscode=‘CSC7710’ AND T1.ssn=T2.ssn)
18 Query 6Return those courses that have never been taught.
33 SQL Solution SELECT P.profname FROM Professor P, Taught T WHERE P.status = ‘full’ AND P.ssn = T.ssn AND T.crscode = ‘CSC6710’
34 Query 12Return the names of full professors who ever taught more than two courses in one semester.
35 SQL Solution SELECT P.profname FROM Professor P WHERE ssn IN( SELECT ssnFROM TaughtGROUP BY ssn, semesterHAVING COUNT(*) > 2)
36 Query 13Delete those professors who never taught a course.
37 SQL Solution DELETE FROM Professor WHERE ssn NOT IN (SELECT ssn FROM Taught)
38 Query 14Change all the credits to 4 for those courses that are taught in f2006 semester.
39 SQL Solution UPDATE Course SET credits = 4 WHERE crscode IN ( SELECT crscodeFROM TaughtWHERE semester = ‘f2006’)
40 Query 15Return the names of the professors who have taught more than 30 credits of courses.
41 SQL Solution SELECT profname FROM Professor WHERE ssn IN ( SELECT T.ssnFROM Taught T, Course CWHERE T.crscode = C.crscodeGROUP BY T.ssnHAVING SUM(C.credits) > 30)
42 Query 16Return the name(s) of the professor(s) who taught the most number of courses in S2006.
43 SQL Solution SELECT profname FROM Professor WHERE ssn IN( SELECT ssn FROM TaughtWHERE semester = ‘S2006’GROUP BY ssnHAVING COUNT(*) =(SELECT MAX(Num)FROM(SELECT ssn, COUNT(*) as NumFROM TaughtGROUP BY ssn))
44 Query 17List all the course names that professor ‘Smith” taught in Fall of 2007.
68 SQL Solution SELECT crscode FROM Course C WHERE c.crscode NOT IN( FROM Taught T, Professor PWHERE T.ssn = P.ssn AND P.status=‘Junior’)
69 Query 27Return the professors who taught the largest number of courses in Fall 2001.
70 SQL Solution SELECT * FROM Professor P1 WHERE Not EXISTS ( SELECT * (SELECT COUNT(*)FROM TaughtWHERE Taught.ssn = P2.ssn AND Taught.semester=‘F2001’)>WHERE Taught.ssn = P1.ssn AND Taught.semester=‘F2001’))
71 Query 28Return the professor who earns the highest salary.
72 SQL Solution SELECT * FROM Professor WHERE salary = ( (SELECT MAX(salary)FROM Professor P)
73 Query 29Return the professor who earns the second highest salary.
74 SQL Solution SELECT * FROM Professor P1 WHERE 1 = ( (SELECT COUNT(*) WHERE P2.salary > P1.salary)Problem: return the professor who earns the 10th highest salary.