Top Banner
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”.
45

Database schema for the exercises

Feb 14, 2022

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

Relational Algebra Solution

crscode(Course)-crscode(Taught)

Professor(ssn, profname, status, salary)

Course(crscode, crsname, credits)

Taught(crscode, semester, ssn)

Page 16: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

SQL Solution

SELECT crscodeFROM TaughtGROUP BY crscodeHAVING COUNT(*) >= 10

Professor(ssn, profname, status, salary)

Course(crscode, crsname, credits)

Taught(crscode, semester, ssn)

Page 21: Database schema for the exercises

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: Database schema for the exercises

Relational Algebra Solution

profname(crscode=‘csc6710’(Taught) Professor)

Professor(ssn, profname, status, salary)

Course(crscode, crsname, credits)

Taught(crscode, semester, ssn)

Page 23: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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: Database schema for the exercises

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)

Page 45: Database schema for the exercises

Thank you!

Q&A