Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relational Algebra and SQL Examples

Similar presentations


Presentation on theme: "Relational Algebra and SQL Examples"— Presentation transcript:

1 Relational Algebra and SQL Examples
Revised based on the notes from Dr. Shiyong Lu of Wayne State University Database Systems: An Application Oriented Approach, Edition 2 by Michael Kifer, Arthur Bernstein, Philip M. Lewis ISBN: ISBN13: Release Date:March 2005

2 Relational Algebra and SQL Exercises
Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) Please refer to the SQLite commands that create the test database.

3 Query 1 Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) Return those professors who have taught ‘CSCI203’ but never ‘CSCI240’.

4 Relational Algebra Solution
Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) ssn(crsname=‘CSCI203’(Taught)) -ssn(crsname=‘CSCI240’(Taught))

5 SQL Solution Professor(ssn, profname, status)
Course(crscode, crsname, credits) Taught(crsname, semester, ssn) SELECT ssn From Taught Where crsname = ‘CSCI203’ EXCEPT Where crsname = ‘CSCI240’;

6 Query 2 Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) Return those professors who have taught both ‘CSCI203’ and ‘CSCI204’.

7 Relational Algebra Solution
Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) ssn(crsname=‘CSCI203’  crsname=‘CSCI204’ (Taught), wrong! ssn(crsname=‘CSCI203’(Taught))  ssn(crsname=‘CSCI204’(Taught)), correct!

8 SQL Solution Professor(ssn, profname, status)
Course(crscode, crsname, credits) Taught(crsname, semester, ssn) SELECT T1.ssn From Taught T1, Taught T2 Where T1.crsname = ‘CSCI203’ AND T2.crsname=‘CSCI204’ AND T1.ssn=T2.ssn;

9 Query 3 Return those professors who have never taught ‘CSCI204’.
Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) Return those professors who have never taught ‘CSCI204’.

10 Relational Algebra Solution
Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) ssn(crsname<>‘CSCI204’(Taught)), wrong answer! ssn(Professor) -ssn(crsname=‘CSCI204’(Taught)), correct answer!

11 SQL Solution Professor(ssn, profname, status)
Course(crscode, crsname, credits) Taught(crsname, semester, ssn) SELECT ssn From Professor EXCEPT From Taught T Where T.crsname = ‘CSCI204’;


Download ppt "Relational Algebra and SQL Examples"

Similar presentations


Ads by Google