Page 1
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 ineach semester; (2) all professors have different salaries;(3) all professors have different names; (4) all courseshave different names; (5) status can take values from“Full”, “Associate”, and “Assistant”.
Page 2
Query 1
Return those professors who have taught ‘csc6710’ but never ‘csc7710’.
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 3
Relational Algebra Solution
ssn(crscode=‘csc6710’(Taught))-ssn(crscode=‘csc7710’(Taught))
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 4
SQL Solution
(SELECT DISTINCT ssnFrom TaughtWhere crscode = ‘CSC6710’)EXCEPT(SELECT DISTINCT ssnFrom TaughtWhere crscode = ‘CSC7710’))
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 5
Query 2
Return those professors who have taught both ‘csc6710’ and ‘csc7710’.
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 6
Relational Algebra Solution
ssn(crscode=‘csc6710’ crscode=‘csc7710’ (Taught))
ssn(crscode=‘csc6710’(Taught)) ssn(crscode=‘csc7710’(Taught))
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
✘
✔
Page 7
SQL Solution
SELECT T1.ssn FROM Taught T1, Taught T2,WHERE T1.crscode = ‘CSC6710’ AND T2.crscode=‘CSC7710’ AND T1.ssn=T2.ssn
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 8
Query 3
Return those professors who have never taught ‘csc7710’.
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 9
Relational Algebra Solution
ssn(crscode<>‘csc7710’(Taught))
ssn(Professor)-ssn(crscode=‘csc7710’(Taught))
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
✘
✔
Page 10
SQL Solution
(SELECT ssn From Professor)EXCEPT(SELECT ssn From Taught TWhere T.crscode = ‘CSC7710’)
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 11
Query 4
Return those professors who taught ‘CSC6710’ and ‘CSC7710” in the same semester
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 12
Relational Algebra Solution
ssn(crscode1=‘csc6710’(Taught[crscode1, ssn, semester]) crscode2=‘csc7710’(Taught[crscode2, ssn, semester]))
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 13
SQL Solution
SELECT T1.ssn FROM Taught T1, Taught T2,WHERE T1.crscode = ‘CSC6710’ AND
T2.crscode=‘CSC7710’ AND T1.ssn=T2.ssn AND T1.semester=T2.semester
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 14
Query 6
Return those courses that have never been taught.
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 15
Relational Algebra Solution
crscode(Course)-crscode(Taught)
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 16
Query 7
Return those courses that have been taught at least in two semesters.
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 17
Relational Algebra Solution
crscode( semester1 <> semester2(
Taught[crscode, ssn1, semester1] Taught[crscode, ssn2, semester2]))
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 18
SQL Solution
SELECT T1.crscodeFROM Taught T1, Taught T2WHERE T1.crscode=T2.crscode AND T1.semester <> T2.semester
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 19
Query 8
Return those courses that have been taught at least in 10 semesters.
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
SQL solution?
Page 20
SQL Solution
SELECT crscodeFROM TaughtGROUP BY crscodeHAVING COUNT(*) >= 10
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 21
Query 10
Return the names of professors who ever taught ‘CSC6710’.
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 22
Relational Algebra Solution
profname(crscode=‘csc6710’(Taught) Professor)
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 23
SQL Solution
SELECT P.profnameFROM Professor P, Taught TWHERE P.ssn = T.ssn AND T.crscode = ‘CSC6710’
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 24
Query 11
Return the names of full professors who ever taught ‘CSC6710’.
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 25
Relational Algebra Solution
profname(crscode=‘csc6710’(Taught) status=‘full’(Professor))
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 26
SQL Solution
SELECT P.profnameFROM Professor P, Taught TWHERE P.status = ‘full’ AND
P.ssn = T.ssn AND T.crscode = ‘CSC6710’
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 27
Query 12
Return the names of full professors who ever taught at least two courses in one semester.
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 28
Relational Algebra Solution
profname(ssn( crscode1 <> crscode2(
Taught[crscode1, ssn, semester] Taught[crscode2, ssn, semester])))
status=‘full’(Professor))
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 29
SQL Solution
SELECT P.profnameFROM Professor P, Taught T1, Taught T2WHERE P.status = ‘Full’ AND
P.ssn = T1.ssn AND T1.ssn = T2.ssn AND T1.crscode <> T2.crscode AND T1.semester = T2.semester
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 30
SQL Solution
SELECT P.profnameFROM Professor PWHERE status = ‘Full’ AND ssn IN(SELECT ssnFROM TaughtGROUP BY ssn, semesterHAVING COUNT(*) >= 2)
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 31
Query 15
Return the names of the professors who have taught more than 30 credits of courses.
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 32
SQL Solution
SELECT profnameFROM ProfessorWHERE ssn IN(
SELECT T.ssnFROM Taught T, Course CWHERE T.crscode = C.crscodeGROUP BY T.ssnHAVING SUM(C.credits) > 30
)
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 33
SQL Solution
SELECT crsnameFROM Professor P, Taught T, Course CWHERE P.profname = ‘Smith’ AND
P.ssn = T.ssn AND T.semester = ‘F2007’ AND T.crscode = C.crscode
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 34
Query 17
List all the course names that professor ‘Smith” taught in Fall of 2007.
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 35
Relational Algebra Solution
crsname(profname=‘Smith’(Professor) semester=‘f2007’(Taught)
Course)
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 36
SQL Solution
SELECT crsnameFROM Professor P, Taught T, Course CWHERE P.profname = ‘Smith’ AND
P.ssn = T.ssn AND T.semester = ‘F2007’ AND T.crscode = C.crscode
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 37
Query 20
Delete those professors who taught less than 10 courses.
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 38
SQL Solution
DELETE FROM ProfessorWHERE ssn IN(
SELECT ssnFROM TaughtGROUP BY ssnHAVING COUNT(*) < 10
)
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 39
Query 21
Delete those professors who taught less than 40 credits.
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 40
SQL Solution
DELETE FROM ProfessorWHERE ssn IN(
SELECT T.ssnFROM Taught T, Course CWHERE T.crscode = C.crscodeGROUP BY ssnHAVING SUM(C.credits) < 40
)
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 41
Query 28
Return the professor who earns the highest salary.
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 42
SQL Solution
SELECT *FROM ProfessorWHERE salary = (
(SELECT MAX(salary)FROM Professor P
)
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 43
Query 29
Return the professor who earns the second highest salary.
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Page 44
SQL Solution
SELECT *FROM Professor P1WHERE 1 = (
(SELECT COUNT(*)FROM Professor P2WHERE P2.salary > P1.salary
)
Problem: return the professor who earns the 10th highest salary.
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)