M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #9 M.P. Johnson Stern School of Business, NYU Spring, 2008
M.P. Johnson, DBMS, Stern/NYU, Spring Agenda Subqueries, etc. Sets, etc.
M.P. Johnson, DBMS, Stern/NYU, Spring From last time: escaping single quotes
M.P. Johnson, DBMS, Stern/NYU, Spring Subqueries Subquery: copy in Conrad’s selection for his ssn: The subquery returns one value, so the = is valid If it returns more (or fewer), we get a run-time error SELECT Purchase.prodname FROM Purchase WHERE buyerssn = (SELECT ssn FROM Person WHERE name = 'Christo') SELECT Purchase.prodname FROM Purchase WHERE buyerssn = (SELECT ssn FROM Person WHERE name = 'Christo')
M.P. Johnson, DBMS, Stern/NYU, Spring Operators on subqueries Several new operators applied to (unary) selections: 1. IN R 2. EXISTS R 3. UNIQUE R 4. s > ALL R 5. s > ANY R 6. x IN R > is just an example op Each expression can be negated with NOT
M.P. Johnson, DBMS, Stern/NYU, Spring Next: ALL op Employees(name, job, divid, salary) Find which employees are paid more than all the programmers SELECT name FROM Employees WHERE salary > ALL (SELECT salary FROM Employees WHERE job='programmer') SELECT name FROM Employees WHERE salary > ALL (SELECT salary FROM Employees WHERE job='programmer')
M.P. Johnson, DBMS, Stern/NYU, Spring ANY/SOME op Employees(name, job, divid, salary) Find which employees are paid more than at least one vice president SELECT name FROM Employees WHERE salary > ANY (SELECT salary FROM Employees WHERE job='VP') SELECT name FROM Employees WHERE salary > ANY (SELECT salary FROM Employees WHERE job='VP')
M.P. Johnson, DBMS, Stern/NYU, Spring ANY/SOME op Employees(name, job, divid, salary) Find which employees are paid more than at least one vice president SELECT name FROM Employees WHERE salary > SOME (SELECT salary FROM Employees WHERE job='VP') SELECT name FROM Employees WHERE salary > SOME (SELECT salary FROM Employees WHERE job='VP')
M.P. Johnson, DBMS, Stern/NYU, Spring Existential/Universal Conditions Employees(name, job, divid, salary) Division(name, id, head) Find all divisions with an employee whose salary is > Existential: easy! SELECT DISTINCT Division.name FROM Employees, Division WHERE salary > AND divid=id SELECT DISTINCT Division.name FROM Employees, Division WHERE salary > AND divid=id
M.P. Johnson, DBMS, Stern/NYU, Spring Existential/Universal Conditions Employees(name, job, divid, salary) Division(name, id, head) Find all divisions in which everyone makes > Existential: easy!
M.P. Johnson, DBMS, Stern/NYU, Spring Existential/universal with IN 2. Select the divisions we didn’t find: 1. Find the other divisions: in which someone makes <= : SELECT name FROM Division WHERE id IN (SELECT divid FROM Employees WHERE salary <= SELECT name FROM Division WHERE id IN (SELECT divid FROM Employees WHERE salary <= SELECT name FROM Division WHERE id NOT IN (SELECT divid FROM Employees WHERE salary <= SELECT name FROM Division WHERE id NOT IN (SELECT divid FROM Employees WHERE salary <=
M.P. Johnson, DBMS, Stern/NYU, Spring Next: correlated subqueries Acc(name,bal,type…) Q: Who has the largest balance? Can we do this with subqueries?
M.P. Johnson, DBMS, Stern/NYU, Spring Acc(name,bal,type,…) Q: Find holder of largest account SELECT name FROM Acc WHERE bal >= ALL (SELECT bal FROM Acc) SELECT name FROM Acc WHERE bal >= ALL (SELECT bal FROM Acc) Correlated Queries
M.P. Johnson, DBMS, Stern/NYU, Spring Correlated Queries So far, subquery executed once; result used for higher query More complicated: correlated queries “[T]he subquery… [is] evaluated many times, once for each assignment of a value to some term in the subquery that comes from a tuple variable outside the subquery” (Ullman, p286). Q: What does this mean? A: That subqueries refer to vars from outer queries
M.P. Johnson, DBMS, Stern/NYU, Spring Acc(name,bal,type,…) Q2: Find holder of largest account of each type SELECT name, type FROM Acc WHERE bal >= ALL (SELECT bal FROM Acc WHERE type=type) SELECT name, type FROM Acc WHERE bal >= ALL (SELECT bal FROM Acc WHERE type=type) Correlated Queries correlation
M.P. Johnson, DBMS, Stern/NYU, Spring Acc(name,bal,type,…) Q2: Find holder of largest account of each type Note: 1. scope of variables 2. this can still be expressed as single SFW SELECT name, type FROM Acc a1 WHERE bal >= ALL (SELECT bal FROM Acc WHERE type=a1.type) SELECT name, type FROM Acc a1 WHERE bal >= ALL (SELECT bal FROM Acc WHERE type=a1.type) Correlated Queries correlation
M.P. Johnson, DBMS, Stern/NYU, Spring New topic: R.A./SQL Set Operators Relations are sets have set-theoretic ops Venn diagrams Union: R1 R2 Example: ActiveEmployees RetiredEmployees Difference: R1 – R2 Example: AllEmployees – RetiredEmployees = ActiveEmployees Intersection: R1 R2 Example: RetiredEmployees UnionizedEmployees
M.P. Johnson, DBMS, Stern/NYU, Spring Set operations - example NameAddressGenderBirthdate Fisher123 MapleF9/9/99 Hamill456 OakM8/8/88 NameAddressGenderBirthdate Fisher123 MapleF9/9/99 Ford345 PalmM7/7/77 R: S: NameAddressGenderBirthdate Fisher123 MapleF9/9/99 Hamill456 OakM8/8/88 Ford345 PalmM7/7/77 R S:
M.P. Johnson, DBMS, Stern/NYU, Spring Set operations - example NameAddressGenderBirthdate Fisher123 MapleF9/9/99 Hamill456 OakM8/8/88 NameAddressGenderBirthdate Fisher123 MapleF9/9/99 Ford345 PalmM7/7/77 R: S: R S: NameAddressGenderBirthdate Fisher123 MapleF9/9/99
M.P. Johnson, DBMS, Stern/NYU, Spring Set operations - example NameAddressGenderBirthdate Fisher123 MapleF9/9/99 Hamill456 OakM8/8/88 NameAddressGenderBirthdate Fisher123 MapleF9/9/99 Ford345 PalmM7/7/77 R: S: R - S: NameAddressGenderBirthdate Hamill456 OakM8/8/88
M.P. Johnson, DBMS, Stern/NYU, Spring Set ops in SQL Orthodox SQL has set operators: UNION, INTERSECT, EXCEPT Oracle SQL uses MINUS rather than EXCEPT See the Ullman page on more differencesUllman These ops applied to queries: (SELECT name FROM Person WHERE City = 'New York') INTERSECT (SELECT custname FROM Purchase WHERE store='Kim''s') (SELECT name FROM Person WHERE City = 'New York') INTERSECT (SELECT custname FROM Purchase WHERE store='Kim''s')
M.P. Johnson, DBMS, Stern/NYU, Spring Boat examples Reserve(ssn,bmodel,color) Q: Find ssns of sailors who reserved red boats or green boats SELECT DISTINCT ssn FROM reserve WHERE color = 'red' OR color = 'green' SELECT DISTINCT ssn FROM reserve WHERE color = 'red' OR color = 'green'
M.P. Johnson, DBMS, Stern/NYU, Spring Boat examples Reserve(ssn,bmodel,color) Q: Find ssns of sailors who reserved red boats and green boats SELECT DISTINCT ssn FROM reserve WHERE color = 'red' AND color = 'green' SELECT DISTINCT ssn FROM reserve WHERE color = 'red' AND color = 'green'
M.P. Johnson, DBMS, Stern/NYU, Spring Boat examples Reserve(ssn,bmodel,color) Q: Find ssns of sailors who reserved red boats and green boats SELECT DISTINCT r1.ssn FROM reserve r1, reserve r2 WHERE r1.ssn = r2.ssn AND r1.color = 'red' AND r2.color = 'green' SELECT DISTINCT r1.ssn FROM reserve r1, reserve r2 WHERE r1.ssn = r2.ssn AND r1.color = 'red' AND r2.color = 'green'
M.P. Johnson, DBMS, Stern/NYU, Spring Boat examples Reserve(ssn,bmodel,color) Q: Find ssns of sailors who reserved red boats and green boats (SELECT DISTINCT ssn FROM reserve WHERE color = 'red') INTERSECT(SELECT DISTINCT ssn FROM reserve WHERE color = 'green') (SELECT DISTINCT ssn FROM reserve WHERE color = 'red') INTERSECT(SELECT DISTINCT ssn FROM reserve WHERE color = 'green')
M.P. Johnson, DBMS, Stern/NYU, Spring Boat examples Reserve(ssn,bmodel,color) Q: Find ssns of sailors who reserved red boats or green boats (SELECT DISTINCT ssn FROM reserve WHERE color = 'red') UNION (SELECT DISTINCT ssn FROM reserve WHERE color = 'green') (SELECT DISTINCT ssn FROM reserve WHERE color = 'red') UNION (SELECT DISTINCT ssn FROM reserve WHERE color = 'green')
M.P. Johnson, DBMS, Stern/NYU, Spring Boat examples Reserve(ssn,bmodel,color) Q: Find ssns of sailors who reserved red boats but not green boats (SELECT DISTINCT ssn FROM reserve WHERE color = 'red') EXCEPT (SELECT DISTINCT ssn FROM reserve WHERE color = 'green') (SELECT DISTINCT ssn FROM reserve WHERE color = 'red') EXCEPT (SELECT DISTINCT ssn FROM reserve WHERE color = 'green')
M.P. Johnson, DBMS, Stern/NYU, Spring (SELECT name, address FROM Cust1) UNION (SELECT name FROM Cust2) (SELECT name, address FROM Cust1) UNION (SELECT name FROM Cust2) Union-Compatibility Situation: Cust1(name,address,…), Cust2(name,…) Want: report of all customer names and addresses (if known) Can’t do: Both tables must have same sequence of types Applies to all set ops
M.P. Johnson, DBMS, Stern/NYU, Spring Union-Compatibility Situation: Cust1(name,address,…), Cust2(name,…) Want: report of all customer names and addresses (if known) But can do: Resulting field names taken from first table (SELECT name, address FROM Cust1) UNION (SELECT name, '(N/A)' FROM Cust2) (SELECT name, address FROM Cust1) UNION (SELECT name, '(N/A)' FROM Cust2) Result(name, address)
M.P. Johnson, DBMS, Stern/NYU, Spring First Unintuitive SQLism Looking for R (S T) But what happens if T is empty? See transcript of this in Oracle on salestranscript SELECTR.A FROM R, S, T WHERER.A=S.A OR R.A=T.A SELECTR.A FROM R, S, T WHERER.A=S.A OR R.A=T.A
M.P. Johnson, DBMS, Stern/NYU, Spring Review Examples from sqlzoo.netsqlzoo.net SELECT L FROM R 1, …, R n WHERE C SELECT L FROM R 1, …, R n WHERE C L ( C (R 1 x … R n )