M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #13 M.P. Johnson Stern School of Business, NYU Spring, 2005
M.P. Johnson, DBMS, Stern/NYU, Spring Agenda Review G&A RegExps Live examples HW2 due next Tuesday MT next Thursday
M.P. Johnson, DBMS, Stern/NYU, Spring General form of G&A S = may contain attributes As and/or any aggregates but no other attributes C1 = condition on the attributes in R 1,…,R n C2 = condition on aggregations or attributes from As Why? NB: “Any attribute of relations in the FROM clause may be aggregated in the HAVING clause, but only those attributes that are in the GROUP BY list may appear unaggregated in the HAVING clause (the same rule as for the SELECT clause)” (Ullman, p283). Why? SELECTS FROMR1,…,Rn WHERE C1 GROUP BYAs HAVINGC2 SELECTS FROMR1,…,Rn WHERE C1 GROUP BYAs HAVINGC2
M.P. Johnson, DBMS, Stern/NYU, Spring Evaluation of G&A Evaluation steps: Compute the FROM-WHERE part as usual to obtain a table with all attributes in R 1,…,R n Group by the attributes a 1,…,a k Compute the aggregates in C 2 and keep only groups satisfying C 2 Compute aggregates in S and return the result SELECTS FROMR 1,…,R n WHERE C 1 GROUP BYa 1,…,a k HAVINGC 2 SELECTS FROMR 1,…,R n WHERE C 1 GROUP BYa 1,…,a k HAVINGC 2
M.P. Johnson, DBMS, Stern/NYU, Spring Summary: SQL queries Only SELECT, FROM required Can’t have HAVING without GROUP BY Can have GROUP BY without HAVING Any clauses used must appear in this order: SELECTL FROMRs WHEREs GROUP BYL2 HAVINGs2 ORDER BYL3 SELECTL FROMRs WHEREs GROUP BYL2 HAVINGs2 ORDER BYL3
M.P. Johnson, DBMS, Stern/NYU, Spring Web page examples Find all authors who wrote at least 10 documents Authors(login, name) Webpages(url, title, login) Attempt 1: with nested queries Bad! SELECT DISTINCT name FROM Authors WHERE (SELECT count(url) FROM Webpages WHERE Authors.login=Webpages.login) > 10 SELECT DISTINCT name FROM Authors WHERE (SELECT count(url) FROM Webpages WHERE Authors.login=Webpages.login) > 10
M.P. Johnson, DBMS, Stern/NYU, Spring Web page examples Find all authors who wrote at least 10 documents: Attempt 2: Simplify with GROUP BY Good! No need for DISTINCT - get for free from GROUP BY SELECT name FROM Authors, Webpages WHERE Authors.login= Webpages.login GROUP BY name HAVING count(Webpages.url) > 10 SELECT name FROM Authors, Webpages WHERE Authors.login= Webpages.login GROUP BY name HAVING count(Webpages.url) > 10
M.P. Johnson, DBMS, Stern/NYU, Spring Web page examples Find all authors who have a vocab > words: Authors(login, name) Webpages(url, title, login) Mentions(url, word) SELECT name FROM Authors, Webpages, Mentions WHERE Authors.login=Wrote.login AND Webpages.url=Mentions.url GROUP BY name HAVING count(distinct word) > SELECT name FROM Authors, Webpages, Mentions WHERE Authors.login=Wrote.login AND Webpages.url=Mentions.url GROUP BY name HAVING count(distinct word) > 10000
M.P. Johnson, DBMS, Stern/NYU, Spring Live examples Inner joins require an ON clause Like a where clause Arbitrary boolean expression If always true (1=1), reduces to cross join New compar op: BETWEEN a between 5 and 10 a >= 5 and a <= 10 Q: produce a list of employees with their salary grades emp, salgrade
M.P. Johnson, DBMS, Stern/NYU, Spring Live examples Q: produce list of bosses and underling- counts, for bosses with >1 underling 1. Ignoring non-bosses 2. Including them 3. Ignoring them again
M.P. Johnson, DBMS, Stern/NYU, Spring sqlzoo exercises 1. Select 2a: 2. Subquery 1d with a subquery: Subquery 1d with a join/cross product:
M.P. Johnson, DBMS, Stern/NYU, Spring Finally: R.A./SQL has limitations Can easily find Alice’s direct subordinates: But: find all of King’s underlings Cannot compute “transitive closure” Cannot express in R.A./SQL! SQL is not “Turing-Complete” NameJobBoss KingPresidentNULL JonesManagerKing BlakeManagerKing FordAnalystJones ScottAnalystJones
M.P. Johnson, DBMS, Stern/NYU, Spring Live examples Examples from sqlzoo.netsqlzoo.net L ( C (R 1 x … R n ) SELECT L FROM R 1, …, R n WHERE C SELECT L FROM R 1, …, R n WHERE C