Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2007.

Similar presentations


Presentation on theme: "CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2007."— Presentation transcript:

1 CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2007

2 2/10/2016Jinze Liu @ University of Kentucky2 Review SFW statement: Sort the outputs: USE bag operation: Enforce set operation: Output average: Partition rows in a table: Select certain groups: Subqueries: ORDER BY Default choice DISTINCT AVG (aggregation function) GROUP BY HAVING =, in, exists

3 2/10/2016Jinze Liu @ University of Kentucky3 Operational semantics of subqueries SELECT * FROM Student AS s WHERE EXISTS (SELECT * FROM Student WHERE name = ’Bart’ AND age = s.age); For each row s in Student Evaluate the subquery with the appropriate value of s.age If the result of the subquery is not empty, output s.* The DBMS query optimizer may choose to process the query in an equivalent, but more efficient way (example?)

4 2/10/2016Jinze Liu @ University of Kentucky4 Exercise Goal: Get the Name of one’s supervisor EMPLOYEE (Eid, Mid, Name) SQL Statement: SELECT EID, Name FROM EMPLOYEE WHERE MId =Eid; SELECT E1.EID, E2.Name FROM EMPLOYEE E1, EMPLOYEE E2 WHERE E1.MId = E2.EId; EidMidName 11231234John Smith 12341311Mary Carter 13111611Bob Lee 14551611Lisa Wang 1611 Jack Snow EidName 1123Mary Carter 1234Bob Lee 1311Jack Snow 1455Jack Snow 1611Jack Snow

5 2/10/2016Jinze Liu @ University of Kentucky5 Exercise Goal: group employees according to their department, for each department, list EIds of its employees and list the head count EMPLOYEE (Eid, Name), DEPARTMENT(Eid, Did) SQL Statement: SELECT Did, Eid, COUNT(Eid) FROM EMPLOYEE e, DEPARTMENT d WHERE e.Eid = d.Eid GROUP BY Did There is something wrong!  Wishful thinking (list a group of Eid after grouping) won’t work  Solution: produce (1) a summary table listing Did and head count, and (2) sort Department according to Did.

6 2/10/2016Jinze Liu @ University of Kentucky6 More on Nested Queries: Scoping To find out which table a column belongs to Start with the immediately surrounding query If not found, look in the one surrounding that; repeat if necessary Use table_name. column_name notation and AS (renaming) to avoid confusion

7 2/10/2016Jinze Liu @ University of Kentucky7 An Example SELECT * FROM Student s WHERE EXISTS (SELECT * FROM Enroll e WHERE SID = s.SID AND EXISTS (SELECT * FROM Enroll WHERE SID = s.SID AND CID <> e.CID)); Students who are taking at least two courses

8 2/10/2016Jinze Liu @ University of Kentucky8 Quantified subqueries A quantified subquery can be used as a value in a WHERE condition Universal quantification (for all): … WHERE x op ALL (subquery) … True iff for all t in the result of subquery, x op t Existential quantification (exists): … WHERE x op ANY (subquery) … True iff there exists some t in the result of subquery such that x op t  Beware In common parlance, “any” and “all” seem to be synonyms In SQL, ANY really means “some”

9 2/10/2016Jinze Liu @ University of Kentucky9 Examples of quantified subqueries Which employees have the highest salary? Employee (Sid, Name, Salary) SELECT * FROM Employee WHERE Salary >= ALL (SELECT Salary FROM Employee); How about the lowest salary? SELECT * FROM Employee WHERE Salary <= ALL (SELECT salary FROM Employee);

10 2/10/2016Jinze Liu @ University of Kentucky10 More ways of getting the highest Salary Who has the highest Salary? SELECT * FROM Employee e WHERE NOT EXISTS (SELECT * FROM Employee WHERE Salary > e.Salary); SELECT * FROM Employee WHERE Eid NOT IN (SELECT e1.SID FROM Employee e1, Employee e2 WHERE e1.Salary < e2.Salary);

11 2/10/2016Jinze Liu @ University of Kentucky11 Nested Queries Nested queries do not add expression power to SQL For convenient Write intuitive SQL queries Can always use SQL queries without nesting to complete the same task (though sometime it is hard)

12 2/10/2016Jinze Liu @ University of Kentucky12 More Exercise Sailors (sid: INTEGER, sname: string, rating: INTEGER, age: REAL) Boats (bid: INTEGER, bname: string, color: string) Reserves (sid: INTEGER, bid: INTEGER) sidsnameratingage 1Fred722 2Jim239 3Nancy827 Sailors sidbid 1102 2 Reserves bidbnamecolor 101Ninared 102Pintablue 103Santa Maria red Boats

13 2/10/2016Jinze Liu @ University of Kentucky13 Exercise I Find sid’s of sailors who’ve reserved a red AND a green boat SELECT R1.sid FROM Boats B1, Reserves R1, Boats B2, Reserves R2 WHERE B1.color=‘red’ AND B2.color=‘green’ AND R1.bid=B1.bid AND R2.bid=B2.bid AND R1.sid=R2.sid SELECT sid FROM Boats, Reserves Where B.color = ‘red’ INTERSECT ( SELECT sid FROM Boats, Reserves Where B.color = ‘green’)

14 2/10/2016Jinze Liu @ University of Kentucky14 Exercise II Find sid’s of sailors who have not reserved a boat SELECT sid FROM Sailors EXCEPT ( SELECT S.sid FROM Sailors S, Reserves R WHERE S.sid=R.sid) Non-monotonic operation!

15 2/10/2016Jinze Liu @ University of Kentucky15 Exercise III (a tough one) Find sailors who’ve reserved all boats SELECT Sid FROM Sailor EXCEPT SELECT Sid FROM (SELECT bid, sid FROM Boat, Sailor EXCEPT Reserves) Non-monotonic operation! #All possible combinations between sid and bid #Existing reservations #Those who do not reserve all boats #All sailors


Download ppt "CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2007."

Similar presentations


Ads by Google