Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 (p#, pname) - Course (c#, prof#, title, credits, room#) - Enroll (stud#, course#) - Room (r#, capacity)

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

3 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 [SELECT (Professor JOIN Course)] JonesStuds = PROJECT [Student JOIN (Enroll JOIN JonesC#s)] PhysicsStuds = PROJECT [Student JOIN (SELECT (Enroll JOIN Course))] Answer = PROJECT [Student JOIN (JonesStuds INTERSECT PhysicsStuds)]

4 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 (Course JOIN Enroll) SameRmStuds = PROJECT (StudCseRm JOIN StudCseRm) AllStuds = PROJECT Student Answer = PROJECT [Student JOIN (AllStuds – SameRmStuds)]

5 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;

6 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;

7 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 table SELECT lastname FROM (SELECT * FROM Employees WHERE ) 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

8 ORT Braude, CSE 61309, ©2004 Gary Schloss Solution 1: use three subqueries SELECT order# FROM (SELECT * FROM Employees WHERE title LIKE ‘Sales %’) E JOIN (SELECT * FROM Orders WHERE shipcountry = ‘France’) O ON E.emp# = O.emp# JOIN (SELECT * FROM Customers WHERE country = ‘France’) C ON O.cust# = C.cust#; Solution 2: use joins SELECT 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’;

9 ORT Braude, CSE 61309, ©2004 Gary Schloss Solution 3: use one subquery SELECT S.order# FROM (SELECT O.order#, E.title, C.country, O.shipcountry FROM Orders O JOIN Employees E ON E.emp# = O.emp# JOIN Customers C ON O.cust# = C.cust#) S WHERE 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…

10 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 ) IF NOT EXISTS (SELECT * FROM table WHERE ) 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


Download ppt "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."

Similar presentations


Ads by Google