Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname,...

74
Relational Algebra and SQL Exercises

Transcript of Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname,...

Page 1: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Relational Algebra and SQL Exercises

Page 2: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

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”.

Page 3: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Query 1

Return those professors who have taught ‘csc6710’ but never ‘csc7710’.

Page 4: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Relational Algebra Solution

ssn(crscode=‘csc6710’(Taught))-ssn(crscode=‘csc7710’(Taught))

Page 5: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

SQL Solution

(SELECT ssn From TaughtWhere crscode = ‘CSC6710’)EXCEPT(SELECT ssn From TaughtWhere crscode = ‘CSC7710’))

Page 6: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Query 2

Return those professors who have taught both ‘csc6710’ and ‘csc7710’.

Page 7: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Relational Algebra Solution

ssn(crscode=‘csc6710’ crscode=‘csc7710’ (Taught), wrong!

ssn(crscode=‘csc6710’(Taught)) ssn(crscode=‘csc7710’(Taught)), correct!

Page 8: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

SQL Solution

SELECT T1.ssn From Taught T1, Taught T2,Where T1.crscode = ‘CSC6710’ AND T2.crscode=‘CSC7710’ AND T1.ssn=T2.ssn

Page 9: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Query 3

Return those professors who have never taught ‘csc7710’.

Page 10: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Relational Algebra Solution

ssn(crscode<>‘csc7710’(Taught)), wrong answer!

ssn(Professor)-ssn(crscode=‘csc7710’(Taught)), correct answer!

Page 11: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

SQL Solution

(SELECT ssn From Professor)EXCEPT(SELECT ssn From Taught TWhere T.crscode = ‘CSC7710’)

Page 12: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Query 4

Return those professors who taught ‘CSC6710’ and ‘CSC7710” in the same semester

Page 13: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Relational Algebra Solution

ssn(crscode1=‘csc6710’(Taught[crscode1, ssn, semester]) crscode2=‘csc7710’(Taught[crscode2, ssn, semester]))

Relational Algebra Solution

Page 14: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

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

Page 15: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Query 5

Return those professors who taught ‘CSC6710’ or ‘CSC7710” but not both.

Page 16: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Relational Algebra Solution

ssn(crscode<>‘csc7710’ crscode=‘csc7710’(Taught))-(ssn(crscode=‘csc6710’(Taught)) ssn(crscode=‘csc7710’(Taught)))

Page 17: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

SQL Solution

(SELECT ssnFROM Taught TWHERE T.crscode=‘CSC6710’ OR T.crscode=‘CSC7710’)Except(SELECT T1.ssn From Taught T1, Taught T2,Where T1.crscode = ‘CSC6710’) AND T2.crscode=‘CSC7710’ AND T1.ssn=T2.ssn)

Page 18: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Query 6

Return those courses that have never been taught.

Page 19: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Relational Algebra Solution

crscode(Course)-crscode(Taught)

Page 20: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

SQL Solution

(SELECT crscodeFROM Course)EXCEPT(SELECT crscodeFROM TAUGHT)

Page 21: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Query 7

Return those courses that have been taught at least in two semesters.

Page 22: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Relational Algebra Solution

crscode( semester1 <> semester2(

Taught[crscode, ssn1, semester1] Taught[crscode, ssn2, semester2]))

Page 23: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

SQL Solution

SELECT T1.crscodeFROM Taught T1, Taught T2WHERE T1.crscode=T2.crscode AND T1.semester <> T2.semester

Page 24: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Query 8

Return those courses that have been taught at least in 10 semesters.

Page 25: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

SQL Solution

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

Page 26: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Query 9

Return those courses that have been taught by at least 5 different professors.

Page 27: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

SQL Solution

SELECT crscodeFROM (SELECT DISTINCT crscode, ssn FROM TAUGHT) GROUP BY crscodeHAVING COUNT(*) >= 5

Page 28: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Query 10

Return the names of professors who ever taught ‘CSC6710’.

Page 29: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Relational Algebra Solution

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

Page 30: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

SQL Solution

SELECT P.profnameFROM Professor P, Taught TWHERE P.ssn = T.ssn AND T.crscode = ‘CSC6710’

Page 31: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Query 11

Return the names of full professors who ever taught ‘CSC6710’.

Page 32: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Relational Algebra Solution

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

Page 33: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

SQL Solution

SELECT P.profnameFROM Professor P, Taught TWHERE P.status = ‘full’ AND P.ssn = T.ssn AND T.crscode = ‘CSC6710’

Page 34: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Query 12

Return the names of full professors who ever taught more than two courses in one semester.

Page 35: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

SQL Solution

SELECT P.profnameFROM Professor PWHERE ssn IN(SELECT ssnFROM TaughtGROUP BY ssn, semesterHAVING COUNT(*) > 2)

Page 36: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Query 13

Delete those professors who never taught a course.

Page 37: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

SQL Solution

DELETE FROM ProfessorWHERE ssn NOT IN(SELECT ssnFROM Taught)

Page 38: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Query 14

Change all the credits to 4 for those courses that are taught in f2006 semester.

Page 39: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

SQL Solution

UPDATE CourseSET credits = 4WHERE crscode IN( SELECT crscode FROM Taught WHERE semester = ‘f2006’)

Page 40: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Query 15

Return the names of the professors who have taught more than 30 credits of courses.

Page 41: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

SQL Solution

SELECT profnameFROM ProfessorWHERE ssn IN( SELECT T.ssn FROM Taught T, Course C WHERE T.crscode = C.crscode GROUP BY T.ssn HAVING SUM(C.credits) > 30)

Page 42: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Query 16

Return the name(s) of the professor(s) who taught the most number of courses in S2006.

Page 43: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

SQL Solution

SELECT profname FROM Professor WHERE ssn IN( SELECT ssn FROM Taught WHERE semester = ‘S2006’ GROUP BY ssn HAVING COUNT(*) = (SELECT MAX(Num) FROM (SELECT ssn, COUNT(*) as Num FROM Taught WHERE semester = ‘S2006’ GROUP BY ssn) ))

Page 44: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Query 17

List all the course names that professor ‘Smith” taught in Fall of 2007.

Page 45: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Relational Algebra Solution

crsname(profname=‘Smith’(Professor) semester=‘f2007’(Taught)

Course)

Page 46: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

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

Page 47: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Query 18

In chronological order, list the number of courses that the professor with ssn ssn = 123456789 taught in each semester.

Page 48: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

SQL Solution

SELECT semester, COUNT(*)FROM TaughtWHERE ssn = ‘123456789’GROUP BY semesterORDER BY semester ASC

Page 49: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Query 19

In alphabetical order of the names of professors, list the name of each professor and the total number of courses she/he has taught.

Page 50: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

SQL Solution

SELECT P.profname, COUNT(*)FROM Professor P, Taught TWHERE P.ssn = T.ssnGROUP BY P.ssn, P.profnameORDER BY P.profname ASC

Page 51: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Query 20

Delete those professors who taught less than 10 courses.

Page 52: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

SQL Solution

DELETE FROM ProfessorWHERE ssn IN( SELECT ssn FROM Taught GROUP BY ssn HAVING COUNT(*) < 10)

Page 53: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Query 21

Delete those professors who taught less than 40 credits.

Page 54: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

SQL Solution

DELETE FROM ProfessorWHERE ssn IN( SELECT T.ssn FROM Taught T, Course C WHERE T.crscode = C.crscode GROUP BY ssn HAVING SUM(C.credits) < 40)

Page 55: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Query 22

List those professors who have not taught any course in the past three semesters (F2006, W2007, F2007).

Page 56: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

SQL Solution

SELECT *FROM Professor PWHERE NOT EXISTS( SELECT * FROM Taught WHERE P.ssn = T.ssn AND (T.semester = ‘F2006’ OR T.semester = ‘W2007’ OR T.semester=‘F2007’)))

Page 57: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Query 23

List the names of those courses that professor Smith have never taught.

Page 58: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Relational Algebra Solution

crsname(Course)-crsname(profname=‘Smith’(Professor) (Taught)

Course)

Page 59: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

SQL Solution

SELECT crsnameFROM Course CWHERE NOT EXISTS SELECT * FROM Professor P, Taught T WHERE P.profname=‘Smith’ AND P.ssn = T.ssn AND T.crscode = C.crscode)

Page 60: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Query 24

Return those courses that have been taught by all professors.

Page 61: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Relational Algebra Solution

crscode, ssn(Taught)/ ssn(Professor)

Page 62: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

SQL Solution

SELECT crscodeFROM Taught T1WHERE NOT EXISTS( (SELECT ssn FROM Professor) EXCEPT (SELECT ssn FROM Taught T2 WHERE T2.crscode = T1.crscode))

Page 63: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Query 25

Return those courses that have been taught in all semesters.

Page 64: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Relational Algebra Solution

crscode, semester(Taught)/ semester(Taught)

Page 65: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

SQL Solution

SELECT crscodeFROM Taught T1WHERE NOT EXISTS( (SELECT semester FROM Taught) EXCEPT (SELECT semester FROM Taught T2 WHERE T2.crscode = T1.crscode))

Page 66: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Query 26

Return those courses that have been taught ONLY by assisitant professors.

Page 67: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Relational Algebra Solution

crscode(Course) - crscode (status‘Assistant’(Professor) Taught)

Page 68: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

SQL Solution

SELECT crscodeFROM Course CWHERE c.crscode NOT IN( (SELECT crscode FROM Taught T, Professor P WHERE T.ssn = P.ssn AND P.status=‘Junior’)

Page 69: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Query 27

Return the professors who taught the largest number of courses in Fall 2001.

Page 70: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

SQL SolutionSELECT *FROM Professor P1WHERE Not EXISTS( SELECT * FROM Professor P2 WHERE( (SELECT COUNT(*) FROM Taught WHERE Taught.ssn = P2.ssn AND Taught.semester=‘F2001’) > (SELECT COUNT(*) FROM Taught WHERE Taught.ssn = P1.ssn AND Taught.semester=‘F2001’))

Page 71: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Query 28

Return the professor who earns the highest salary.

Page 72: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

SQL Solution

SELECT *FROM ProfessorWHERE salary = ( (SELECT MAX(salary) FROM Professor P)

Page 73: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

Query 29

Return the professor who earns the second highest salary.

Page 74: Relational Algebra and SQL Exercises. Database schema for the exercises Professor(ssn, profname, status, salary) Course(crscode, crsname, credits) Taught(crscode,

SQL Solution

SELECT *FROM Professor P1WHERE 1 = ( (SELECT COUNT(*) FROM Professor P2 WHERE P2.salary > P1.salary)

Problem: return the professor who earns the 10th highest salary.