cs 405g: introduction to database systems instructor: jinze liu fall 2007
DESCRIPTION
2/10/2016Jinze 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?)TRANSCRIPT
CS 405G: Introduction to Database Systems
Instructor: Jinze Liu
Fall 2007
05/03/23 Jinze Liu @ University of Kentucky 2
Review SFW statement: Sort the outputs: USE bag operation: Enforce set operation: Output average: Partition rows in a table: Select certain groups: Subqueries:
ORDER BYDefault choiceDISTINCTAVG (aggregation function)GROUP BYHAVING=, in, exists
05/03/23 Jinze Liu @ University of Kentucky 3
Operational semantics of subqueries SELECT *
FROM Student AS sWHERE 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?)
05/03/23 Jinze Liu @ University of Kentucky 4
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;
Eid Mid Name
1123 1234 John Smith
1234 1311 Mary Carter
1311 1611 Bob Lee
1455 1611 Lisa Wang
1611 1611 Jack Snow
Eid Name
1123 Mary Carter
1234 Bob Lee
1311 Jack Snow
1455 Jack Snow
1611 Jack Snow
05/03/23 Jinze Liu @ University of Kentucky 5
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.EidGROUP 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.
05/03/23 Jinze Liu @ University of Kentucky 6
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
05/03/23 Jinze Liu @ University of Kentucky 7
An Example
SELECT * FROM Student sWHERE 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
05/03/23 Jinze Liu @ University of Kentucky 8
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”
05/03/23 Jinze Liu @ University of Kentucky 9
Examples of quantified subqueries Which employees have the highest salary?
Employee (Sid, Name, Salary) SELECT *FROM EmployeeWHERE Salary >= ALL (SELECT Salary FROM Employee);
How about the lowest salary? SELECT *FROM EmployeeWHERE Salary <= ALL (SELECT salary FROM Employee);
05/03/23 Jinze Liu @ University of Kentucky 10
More ways of getting the highest Salary
Who has the highest Salary? SELECT * FROM Employee eWHERE 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);
05/03/23 Jinze Liu @ University of Kentucky 11
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)
05/03/23 Jinze Liu @ University of Kentucky 12
More ExerciseSailors (sid: INTEGER, sname: string, rating: INTEGER, age:
REAL)Boats (bid: INTEGER, bname: string, color: string)Reserves (sid: INTEGER, bid: INTEGER)
sid sname rating age1 Fred 7 222 Jim 2 393 Nancy 8 27
Sailors
sid bid1 1022 102
Reserves
bid bname color101 Nina red102 Pinta blue103 Santa
Mariared
Boats
05/03/23 Jinze Liu @ University of Kentucky 13
Exercise I Find sid’s of sailors who’ve reserved a red AND a green boat SELECT R1.sidFROM Boats B1, Reserves R1, Boats B2, Reserves R2WHERE 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, ReservesWhere B.color = ‘red’INTERSECT (SELECT sid FROM Boats, ReservesWhere B.color = ‘green’)
05/03/23 Jinze Liu @ University of Kentucky 14
Exercise II Find sid’s of sailors who have not reserved a boatSELECT sidFROM SailorsEXCEPT(SELECT S.sidFROM Sailors S, Reserves RWHERE S.sid=R.sid)
Non-monotonic operation!
05/03/23 Jinze Liu @ University of Kentucky 15
Exercise III (a tough one) Find sailors who’ve reserved all boatsSELECT SidFROM Sailor
EXCEPTSELECT SidFROM
(SELECT bid, sidFROM Boat, SailorEXCEPTReserves)
Non-monotonic operation!
#All possible combinations between sid and bid
#Existing reservations
#Those who do not reserve all boats
#All sailors