Presentation is loading. Please wait.

Presentation is loading. Please wait.

M.P. Johnson, DBMS, Stern/NYU, Spring 20061 C20.0046: Database Management Systems Lecture #13 M.P. Johnson Stern School of Business, NYU Spring, 2005.

Similar presentations


Presentation on theme: "M.P. Johnson, DBMS, Stern/NYU, Spring 20061 C20.0046: Database Management Systems Lecture #13 M.P. Johnson Stern School of Business, NYU Spring, 2005."— Presentation transcript:

1 M.P. Johnson, DBMS, Stern/NYU, Spring 20061 C20.0046: Database Management Systems Lecture #13 M.P. Johnson Stern School of Business, NYU Spring, 2005

2 M.P. Johnson, DBMS, Stern/NYU, Spring 2006 2 Agenda Review G&A RegExps Live examples HW2 due next Tuesday MT next Thursday

3 M.P. Johnson, DBMS, Stern/NYU, Spring 2006 3 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

4 M.P. Johnson, DBMS, Stern/NYU, Spring 2006 4 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

5 M.P. Johnson, DBMS, Stern/NYU, Spring 2006 5 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

6 M.P. Johnson, DBMS, Stern/NYU, Spring 2006 6 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

7 M.P. Johnson, DBMS, Stern/NYU, Spring 2006 7 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

8 M.P. Johnson, DBMS, Stern/NYU, Spring 2006 8 Web page examples Find all authors who have a vocab > 10000 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) > 10000 SELECT name FROM Authors, Webpages, Mentions WHERE Authors.login=Wrote.login AND Webpages.url=Mentions.url GROUP BY name HAVING count(distinct word) > 10000

9 M.P. Johnson, DBMS, Stern/NYU, Spring 2006 9 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

10 M.P. Johnson, DBMS, Stern/NYU, Spring 2006 10 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

11 M.P. Johnson, DBMS, Stern/NYU, Spring 2006 11 sqlzoo exercises 1. Select 2a: http://sqlzoo.net/1.htmhttp://sqlzoo.net/1.htm 2. Subquery 1d with a subquery: http://sqlzoo.net/1a.htm http://sqlzoo.net/1a.htm 3. Subquery 1d with a join/cross product: http://sqlzoo.net/1a.htm http://sqlzoo.net/1a.htm

12 M.P. Johnson, DBMS, Stern/NYU, Spring 2006 12 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

13 M.P. Johnson, DBMS, Stern/NYU, Spring 2006 13 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


Download ppt "M.P. Johnson, DBMS, Stern/NYU, Spring 20061 C20.0046: Database Management Systems Lecture #13 M.P. Johnson Stern School of Business, NYU Spring, 2005."

Similar presentations


Ads by Google