Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 SQL Additional Notes. 2  1 Group and Aggregation*  2 Execution Order*  3 Join*  4 Find the maximum  5 Line Format SQL Additional Notes *partially.

Similar presentations


Presentation on theme: "1 SQL Additional Notes. 2  1 Group and Aggregation*  2 Execution Order*  3 Join*  4 Find the maximum  5 Line Format SQL Additional Notes *partially."— Presentation transcript:

1 1 SQL Additional Notes

2 2  1 Group and Aggregation*  2 Execution Order*  3 Join*  4 Find the maximum  5 Line Format SQL Additional Notes *partially from University of Washington CSE 554

3 Grouping and Aggregation Purchase(product, date, price, quantity) SELECT product, Sum(price*quantity) AS TotalSales FROM Purchase WHERE date > ‘10/1/2005’ GROUP BY product SELECT product, Sum(price*quantity) AS TotalSales FROM Purchase WHERE date > ‘10/1/2005’ GROUP BY product Let’s see what this means… Find total sales after 10/1/2005 per product.

4 1&2. FROM-WHERE-GROUPBY ProductDatePriceQuantity Bagel10/21120 Bagel10/251.5020 Banana10/30.510 Banana10/10110

5 3. SELECT SELECT product, Sum(price*quantity) AS TotalSales FROM Purchase WHERE date > ‘10/1/2005’ GROUP BY product SELECT product, Sum(price*quantity) AS TotalSales FROM Purchase WHERE date > ‘10/1/2005’ GROUP BY product ProductDatePriceQuantity Bagel10/21120 Bagel10/251.5020 Banana10/30.510 Banana10/10110 ProductTotalSales Bagel50 Banana15

6 GROUP BY v.s. Nested Quereis SELECT product, Sum(price*quantity) AS TotalSales FROM Purchase WHERE date > ‘10/1/2005’ GROUP BY product SELECT product, Sum(price*quantity) AS TotalSales FROM Purchase WHERE date > ‘10/1/2005’ GROUP BY product SELECT DISTINCT x.product, (SELECT Sum(y.price*y.quantity) FROM Purchase y WHERE x.product = y.product AND y.date > ‘10/1/2005’) AS TotalSales FROM Purchase x WHERE x.date > ‘10/1/2005’ SELECT DISTINCT x.product, (SELECT Sum(y.price*y.quantity) FROM Purchase y WHERE x.product = y.product AND y.date > ‘10/1/2005’) AS TotalSales FROM Purchase x WHERE x.date > ‘10/1/2005’

7 HAVING Clause SELECT product, Sum(price * quantity) FROM Purchase WHERE date > ‘10/1/2005’ GROUP BY product HAVING Sum(quantity) > 30 SELECT product, Sum(price * quantity) FROM Purchase WHERE date > ‘10/1/2005’ GROUP BY product HAVING Sum(quantity) > 30 Same query, except that we consider only products that had at least 30 quantity sale HAVING clause contains conditions on aggregates.

8 General form of Grouping and Aggregation SELECT S FROM R 1,…,R n WHERE C1 GROUP BY a 1,…,a k HAVING C2 S = may contain attributes a 1,…,a k and/or any aggregates but NO OTHER ATTRIBUTES C1 = is any condition on the attributes in R 1,…,R n C2 = is any condition on aggregate expressions

9 Execution Order Evaluation steps: 1. Evaluate FROM-WHERE, apply condition C1 2. Group by the attributes a 1,…,a k 3. Apply condition C2 to each group (may have aggregates) 4. Compute aggregates in S and return the result SELECT S FROM R 1,…,R n WHERE C1 GROUP BY a 1,…,a k HAVING C2 SELECT S FROM R 1,…,R n WHERE C1 GROUP BY a 1,…,a k HAVING C2

10 Join Explicit joins in SQL = “inner joins”: Product(name, category) Purchase(prodName, store) SELECT Product.name, Purchase.store FROM Product JOIN Purchase ON Product.name = Purchase.prodName SELECT Product.name, Purchase.store FROM Product JOIN Purchase ON Product.name = Purchase.prodName SELECT Product.name, Purchase.store FROM Product, Purchase WHERE Product.name = Purchase.prodName SELECT Product.name, Purchase.store FROM Product, Purchase WHERE Product.name = Purchase.prodName Same as: But Products that never sold will be lost !

11 Outerjoins Left outer joins in SQL: Product(name, category) Purchase(prodName, store) SELECT Product.name, Purchase.store FROM Product LEFT OUTER JOIN Purchase ON Product.name = Purchase.prodName SELECT Product.name, Purchase.store FROM Product LEFT OUTER JOIN Purchase ON Product.name = Purchase.prodName

12 NameCategory Gizmogadget CameraPhoto OneClickPhoto ProdNameStore GizmoWiz CameraRitz CameraWiz NameStore GizmoWiz CameraRitz CameraWiz OneClickNULL ProductPurchase

13 Outer Joins  Left outer join:  Include the left tuple even if there’s no match  Right outer join:  Include the right tuple even if there’s no match  Full outer join:  Include the both left and right tuples even if there’s no match

14 Find the maximum  Find student with highest grade  Method 1 select student, grade from tableA where grade = (select max(grade) from tableA)  Method 2 StudentGrade A90 B100 C80 select student, grade from tableA where grade = (select max(grade) from tableA) select student, grade from tableA where grade = (select max(grade) from tableA) select * from( select student, grade from tableA order by Grade DESC ) where ROWNUM <=1 select * from( select student, grade from tableA order by Grade DESC ) where ROWNUM <=1 tableA

15 Find the maximum  What if they have a tie Method 2 will only give the first row which is (A,100)  Method 3 *We will not have ties in grading of Project 2 StudentGrade A100 B C80 select * from( select student, rank() over (order by grade desc) as RNK from tableA ) where RNK <=1 select * from( select student, rank() over (order by grade desc) as RNK from tableA ) where RNK <=1

16 Format  You will not lose points because of format  the tabs, spaces, center or left alignment.  But a good format is preferred.  Easier to grade and good for further development  To avoid line warp  When creating table, use appropriate column size. For instance, sname varchar(50) instead of varchar(255)  SET LINESIZE 300

17 Questions? 17


Download ppt "1 SQL Additional Notes. 2  1 Group and Aggregation*  2 Execution Order*  3 Join*  4 Find the maximum  5 Line Format SQL Additional Notes *partially."

Similar presentations


Ads by Google