Presentation is loading. Please wait.

Presentation is loading. Please wait.

Subqueries CIT 381 2001 Oct 26. Nesting A query can be put inside another query Most commonly in the WHERE clause Sometimes in the FROM clause (depending.

Similar presentations


Presentation on theme: "Subqueries CIT 381 2001 Oct 26. Nesting A query can be put inside another query Most commonly in the WHERE clause Sometimes in the FROM clause (depending."— Presentation transcript:

1 Subqueries CIT 381 2001 Oct 26

2 Nesting A query can be put inside another query Most commonly in the WHERE clause Sometimes in the FROM clause (depending on the software) This subquery is executed first (if possible)

3 % Find the average sales of the Sales Reps SELECT AVG(sales) FROM salesreps; Expr1000 $289,353.20 % Find those Sales Reps whose sales exceed the average SELECT name, sales FROM salesreps WHERE sales > (SELECT AVG(sales) FROM salesreps); namesales Dan Roberts$305,673.00 Sue Smith$474,050.00 Bill Adams$367,911.00 Sam Clark$299,912.00 Larry Fitch$361,865.00 Mary Jones$392,725.00

4 % Find the Sales Rep with maximum sales Two ways: SELECT name, sales FROM salesreps WHERE sales = (SELECT MAX(sales) FROM salesreps); -or- SELECT name, sales FROM salesreps WHERE sales >= ALL (SELECT sales FROM salesreps); namesales Sue Smith$474,050.00

5 Correlated vs Uncorrelated The previous subqueries did not depend on anything outside the subquery …and thus need to be executed just once. There are called uncorrelated. A correlated subquery depends on data from the outer query … and thus has to be executed for each row of the outer table(s)

6 Correlated Example % For each office, find the Sales Rep with the most sales in that office. SELECT s_out.name, s_out.sales FROM salesreps s_out WHERE s_out.sales= (SELECT MAX(s_in.sales) FROM salesreps s_in WHERE s_in.rep_office=s_out.rep_office) namesales Dan Roberts$305,673.00 Sue Smith$474,050.00 Bill Adams$367,911.00 Nancy Angelli$186,042.00 Mary Jones$392,725.00

7 Macho SQL SELECT s1.rep_office, s1.name, s1.sales FROM salesreps AS s1, (SELECT rep_office, MAX(sales) AS ms FROM salesreps GROUP BY rep_office) AS s2 WHERE s1.sales=s2.ms AND s1.rep_office=s2.rep_office rep_officenamesales 12Dan Roberts$305,673.00 21Sue Smith$474,050.00 13Bill Adams$367,911.00 22Nancy Angelli$186,042.00 11Mary Jones$392,725.00 The subquery could be viewed as a table and put in the FROM clause.

8 In the previous lecture we wrote a query for % Get the average and total sales of those Reps with over $30k in total sales. Someone asked: “How do we get the sum of those total sales?” That is, sum the sales of all those who exceed $30,000 in sales. We can (1) use a subquery in the FROM clause Or (2) use a TEMP table. (Solution 2 not in Access??)

9 Solution 1 SELECT SUM(TotalSales) FROM (SELECT s.name, AVG(o.amount) as AvgSales, SUM(o.amount) AS TotalSales FROM salesreps s, orders o WHERE s.empl_num=o.rep GROUP BY s.name HAVING SUM(o.amount)>=30000); Expr1000 $165,350.00

10 Subqueries as Sets A subquery can be viewed as a set (in the WHERE clause) Viewed as a table in the FROM clause (more rare and less supported) As sets, can use set operations IN, EXISTS, ANY, ALL

11 Simulate a Join % Find all managers. SELECT name FROM salesreps WHERE empl_num IN (SELECT manager FROM salesreps) You could do this as a self-join. That would be more efficient, because join computations are optimized. name Dan Roberts Bob Smith Sam Clark Larry Fitch

12 Tricky NOT IN % Find those people who are not managers. SELECT name FROM salesreps WHERE empl_num NOT IN (SELECT manager FROM salesreps) OOPS (try it). This gives no names. Obviously the wrong answer.

13 Look at contents of SALESREPS EMPL_NUMNAMEAGEREP_OFFICETITLEHIRE_DATEMANAGERQUOTASALES 101Dan Roberts4512Sales Rep20-Oct-86104$300,000.00$305,673.00 102Sue Smith4821Sales Rep10-Dec-86108$350,000.00$474,050.00 103Paul Cruz2912Sales Rep01-Mar-87104$275,000.00$286,775.00 104Bob Smith3312Sales Mgr19-May-87106$200,000.00$142,594.00 105Bill Adams3713Sales Rep12-Feb-88104$350,000.00$367,911.00 106Sam Clark5211VP Sales14-Jun-88$275,000.00$299,912.00 107Nancy Angelli4922Sales Rep14-Nov-88108$300,000.00$186,042.00 108Larry Fitch6221Sales Mgr12-Oct-89106$350,000.00$361,865.00 109Mary Jones3111Sales Rep12-Oct-89106$300,000.00$392,725.00 110Tom Snyder41Sales Rep13-Jan-90101$75,985.00

14 Tricky NULLS again One of the managers is NULL. That could be anybody (so we can’t say anyone is not in there). SELECT name FROM salesreps WHERE empl_num NOT IN (SELECT manager FROM salesreps WHERE manager IS NOT NULL) name Sue Smith Paul Cruz Bill Adams Nancy Angelli Mary Jones Tom Snyder


Download ppt "Subqueries CIT 381 2001 Oct 26. Nesting A query can be put inside another query Most commonly in the WHERE clause Sometimes in the FROM clause (depending."

Similar presentations


Ads by Google