ort braude, cse 61309, ©2004 gary schloss exam question #1: er, ra & sql consider a db schema...

10
ORT Braude, CSE 61309, ©2004 Gary Schloss Exam Question #1: ER, RA & SQL Consider a DB schema with the following relations: - Student (s# , sname) - Professor (p# , pname) - Course (c# , prof#, title, credits, room#) - Enroll (stud#, course#) - Room (r# , capacity)

Upload: edith-lee

Post on 19-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ORT Braude, CSE 61309, ©2004 Gary Schloss Exam Question #1: ER, RA & SQL Consider a DB schema with the following relations: - Student (s#, sname) - Professor

ORT Braude, CSE 61309, ©2004 Gary Schloss

Exam Question #1: ER, RA & SQL

Consider a DB schema with the following relations:

- Student (s#, sname)

- Professor (p#, pname)

- Course (c#, prof#, title, credits, room#)

- Enroll (stud#, course#)

- Room (r#, capacity)

Page 2: ORT Braude, CSE 61309, ©2004 Gary Schloss Exam Question #1: ER, RA & SQL Consider a DB schema with the following relations: - Student (s#, sname) - Professor

ORT Braude, CSE 61309, ©2004 Gary Schloss

•Draw an ER model of that database. Make sure to indicate that

each course is taught by one professor and in one classroom.

Page 3: ORT Braude, CSE 61309, ©2004 Gary Schloss Exam Question #1: ER, RA & SQL Consider a DB schema with the following relations: - Student (s#, sname) - Professor

ORT Braude, CSE 61309, ©2004 Gary Schloss

Exam Question #1 (contd.)

• Write an RA expression that finds the names of all students who are enrolled in a class taught by Professor Jones, and are enrolled in a Physics class.

JonesC#s = PROJECT<c#> [SELECT<pname="Jones"> (Professor JOIN<p#=prof#> Course)]

JonesStuds = PROJECT<stud#> [Student JOIN<s#=stud#> (Enroll JOIN<c#=course#> JonesC#s)]

PhysicsStuds = PROJECT<stud#> [Student JOIN<s#=stud#> (SELECT<title="Physics"> (Enroll JOIN<course#=c#> Course))]

Answer = PROJECT<sname> [Student JOIN<s#=stud#> (JonesStuds INTERSECT PhysicsStuds)]

Page 4: ORT Braude, CSE 61309, ©2004 Gary Schloss Exam Question #1: ER, RA & SQL Consider a DB schema with the following relations: - Student (s#, sname) - Professor

ORT Braude, CSE 61309, ©2004 Gary Schloss

Exam Question #1 (contd.)

• Write an RA expression that finds the names of all students who are not enrolled in two classes held in the same room.

StudCseRm = PROJECT<s#, c#, r#> (Course JOIN<c#=course#> Enroll)

SameRmStuds = PROJECT<stud#> (StudCseRm JOIN <s#=stud# & r#=room# & c# != course#>

StudCseRm)

AllStuds = PROJECT<s#> Student

Answer = PROJECT<sname> [Student JOIN<s#=stud#> (AllStuds – SameRmStuds)]

Page 5: ORT Braude, CSE 61309, ©2004 Gary Schloss Exam Question #1: ER, RA & SQL Consider a DB schema with the following relations: - Student (s#, sname) - Professor

ORT Braude, CSE 61309, ©2004 Gary Schloss

Exam Question #1 (contd.)

• Write an SQL query that lists, in alphabetical order, the title of all the courses either taught by Prof. Smith or are taught in room 44. Do not list duplicate titles.

SELECT DISTINCT title FROM Course

WHERE c# IN

(SELECT c# FROM Professor, Course WHERE p# = prof# AND pname = “Smith”)

OR room# = 44

ORDER BY title;

Page 6: ORT Braude, CSE 61309, ©2004 Gary Schloss Exam Question #1: ER, RA & SQL Consider a DB schema with the following relations: - Student (s#, sname) - Professor

ORT Braude, CSE 61309, ©2004 Gary Schloss

Exam Question #1 (contd.)

• Write an SQL query that considers all the courses that have ever been taught by Prof. Brown and are of 3 credits, and groups them according to title. For each course, the query gives its title and the average capacity of rooms in which the course was offered, and only courses with average room capacity of greater than 20 are listed.

SELECT title, AVG(capacity) FROM Room, Course

WHERE r# = room# AND credits = 3 AND c# IN

(SELECT c# FROM Professor, Course WHERE p# = prof# AND pname = “Brown”)

GROUP BY title

HAVING AVG(capacity) > 20;

Page 7: ORT Braude, CSE 61309, ©2004 Gary Schloss Exam Question #1: ER, RA & SQL Consider a DB schema with the following relations: - Student (s#, sname) - Professor

ORT Braude, CSE 61309, ©2004 Gary Schloss

Reminder: Use of Subqueries

• To build a dynamic WHERE clause like above:WHERE Column IN (Subquery)

• To reference a derived tableSELECT lastname FROM

(SELECT * FROM Employees WHERE <condition>) AS E

– E is a data set returned to the outer query and can now be used for other SQL functions

• Example: Find all orders for items from France that were handled by sales employees and were sold to customers in France

Page 8: ORT Braude, CSE 61309, ©2004 Gary Schloss Exam Question #1: ER, RA & SQL Consider a DB schema with the following relations: - Student (s#, sname) - Professor

ORT Braude, CSE 61309, ©2004 Gary Schloss

• Solution 1: use three subqueriesSELECT order# FROM

(SELECT * FROM Employees WHERE title LIKE ‘Sales %’) EJOIN

(SELECT * FROM Orders WHERE shipcountry = ‘France’) OON E.emp# = O.emp#

JOIN (SELECT * FROM Customers WHERE country = ‘France’) CON O.cust# = C.cust#;

• Solution 2: use joinsSELECT order# FROM Orders O

JOIN Employees E ON E.emp# = O.emp#

JOIN Customers C ON O.cust# = C.cust#

WHERE E.title LIKE ‘Sales %’AND O.shipcountry = ‘France’AND C.country = ‘France’;

Page 9: ORT Braude, CSE 61309, ©2004 Gary Schloss Exam Question #1: ER, RA & SQL Consider a DB schema with the following relations: - Student (s#, sname) - Professor

ORT Braude, CSE 61309, ©2004 Gary Schloss

• Solution 3: use one subquerySELECT S.order# FROM

(SELECT O.order#, E.title, C.country, O.shipcountry FROM Orders OJOIN Employees E ON E.emp# = O.emp#JOIN Customers C ON O.cust# = C.cust#) SWHERE S.title LIKE ‘Sales %’

AND S.shipcountry = ‘France’AND S.country = ‘France’;

• For all three solutions, the Query Analyzer will generate exactly the same query plan.

• So, which approach is best? Answer: It Depends…

Page 10: ORT Braude, CSE 61309, ©2004 Gary Schloss Exam Question #1: ER, RA & SQL Consider a DB schema with the following relations: - Student (s#, sname) - Professor

ORT Braude, CSE 61309, ©2004 Gary Schloss

A Few Useful Hints

• To count number of tuples, use:SELECT COUNT(*) AS RowCount FROM table;SELECT COUNT(DISTINCT ProdName) FROM table;

• If in a subquery a dynamically determined data set may return empty, use:

IF EXISTS (SELECT * FROM table WHERE <condition>)IF NOT EXISTS (SELECT * FROM table WHERE <condition>)

• The order in which an SQL statement is evaluated:(1) FROM(2) WHERE;(3) Aggregate Functions (MAX, MIN, SUM, AVG, COUNT) and GROUP BY(4) HAVING(5) ORDER BY(6) SELECT