database schema for the exercises
Post on 14-Feb-2022
19 Views
Preview:
TRANSCRIPT
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”.
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)
Relational Algebra Solution
ssn(crscode=‘csc6710’(Taught))-ssn(crscode=‘csc7710’(Taught))
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
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)
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)
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)
✘
✔
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)
Query 3
Return those professors who have never taught ‘csc7710’.
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
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)
✘
✔
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)
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)
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)
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)
Query 6
Return those courses that have never been taught.
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Relational Algebra Solution
crscode(Course)-crscode(Taught)
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
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)
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)
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)
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?
SQL Solution
SELECT crscodeFROM TaughtGROUP BY crscodeHAVING COUNT(*) >= 10
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Query 10
Return the names of professors who ever taught ‘CSC6710’.
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
Relational Algebra Solution
profname(crscode=‘csc6710’(Taught) Professor)
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
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)
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)
Relational Algebra Solution
profname(crscode=‘csc6710’(Taught) status=‘full’(Professor))
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
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)
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)
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)
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)
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)
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)
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)
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)
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)
Relational Algebra Solution
crsname(profname=‘Smith’(Professor) semester=‘f2007’(Taught)
Course)
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
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)
Query 20
Delete those professors who taught less than 10 courses.
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
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)
Query 21
Delete those professors who taught less than 40 credits.
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
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)
Query 28
Return the professor who earns the highest salary.
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
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)
Query 29
Return the professor who earns the second highest salary.
Professor(ssn, profname, status, salary)
Course(crscode, crsname, credits)
Taught(crscode, semester, ssn)
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)
top related