17
1 SQL Additional Notes

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

Embed Size (px)

Citation preview

Page 1: 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

1

SQL Additional Notes

Page 2: 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

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

Page 3: 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

Grouping and Aggregation

Purchase(product, date, price, quantity)

SELECT product, Sum(price*quantity) AS TotalSalesFROM PurchaseWHERE date > ‘10/1/2005’GROUP BY product

SELECT product, Sum(price*quantity) AS TotalSalesFROM PurchaseWHERE date > ‘10/1/2005’GROUP BY product

Let’s see what this means…

Find total sales after 10/1/2005 per product.

Page 4: 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

1&2. FROM-WHERE-GROUPBY

Product Date Price Quantity

Bagel 10/21 1 20

Bagel 10/25 1.50 20

Banana 10/3 0.5 10

Banana 10/10 1 10

Page 5: 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

3. SELECT

SELECT product, Sum(price*quantity) AS TotalSalesFROM PurchaseWHERE date > ‘10/1/2005’GROUP BY product

SELECT product, Sum(price*quantity) AS TotalSalesFROM PurchaseWHERE date > ‘10/1/2005’GROUP BY product

Product Date Price Quantity

Bagel 10/21 1 20

Bagel 10/25 1.50 20

Banana 10/3 0.5 10

Banana 10/10 1 10

Product TotalSales

Bagel 50

Banana 15

Page 6: 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

GROUP BY v.s. Nested Quereis

SELECT product, Sum(price*quantity) AS TotalSalesFROM PurchaseWHERE date > ‘10/1/2005’GROUP BY product

SELECT product, Sum(price*quantity) AS TotalSalesFROM PurchaseWHERE 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 TotalSalesFROM Purchase xWHERE 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 TotalSalesFROM Purchase xWHERE x.date > ‘10/1/2005’

Page 7: 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

HAVING Clause

SELECT product, Sum(price * quantity)FROM PurchaseWHERE date > ‘10/1/2005’GROUP BY productHAVING Sum(quantity) > 30

SELECT product, Sum(price * quantity)FROM PurchaseWHERE date > ‘10/1/2005’GROUP BY productHAVING Sum(quantity) > 30

Same query, except that we consider only products that hadat least 30 quantity sale

HAVING clause contains conditions on aggregates.

Page 8: 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

General form of Grouping and Aggregation

SELECT SFROM R1,…,Rn

WHERE C1GROUP BY a1,…,ak

HAVING C2

S = may contain attributes a1,…,ak and/or any aggregates but NO OTHER ATTRIBUTES

C1 = is any condition on the attributes in R1,…,Rn

C2 = is any condition on aggregate expressions

Page 9: 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

Execution Order

Evaluation steps:

1. Evaluate FROM-WHERE, apply condition C1

2. Group by the attributes a1,…,ak

3. Apply condition C2 to each group (may have aggregates)

4. Compute aggregates in S and return the result

SELECT SFROM R1,…,Rn

WHERE C1GROUP BY a1,…,ak

HAVING C2

SELECT SFROM R1,…,Rn

WHERE C1GROUP BY a1,…,ak

HAVING C2

Page 10: 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

Join

Explicit joins in SQL = “inner joins”: Product(name, category)

Purchase(prodName, store)

SELECT Product.name, Purchase.storeFROM Product JOIN Purchase ON Product.name = Purchase.prodName

SELECT Product.name, Purchase.storeFROM Product JOIN Purchase ON Product.name = Purchase.prodName

SELECT Product.name, Purchase.storeFROM Product, PurchaseWHERE Product.name = Purchase.prodName

SELECT Product.name, Purchase.storeFROM Product, PurchaseWHERE Product.name = Purchase.prodName

Same as:

But Products that never sold will be lost !

Page 11: 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

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

Page 12: 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

Name Category

Gizmo gadget

Camera Photo

OneClick Photo

ProdName Store

Gizmo Wiz

Camera Ritz

Camera Wiz

Name Store

Gizmo Wiz

Camera Ritz

Camera Wiz

OneClick NULL

Product Purchase

Page 13: 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

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

Page 14: 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

Find the maximum

Find student with highest grade Method 1

select student, grade

from tableA

where grade = (select max(grade) from tableA) Method 2

Student Grade

A 90

B 100

C 80

select student, gradefrom tableAwhere grade = (select max(grade) from tableA)

select student, gradefrom tableAwhere grade = (select max(grade) from tableA)

select * from(select student, gradefrom tableAorder by Grade DESC

) where ROWNUM <=1

select * from(select student, gradefrom tableAorder by Grade DESC

) where ROWNUM <=1

tableA

Page 15: 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

Find the maximum

What if they have a tieMethod 2 will only give the first

row which is (A,100)

Method 3

*We will not have ties in grading of Project 2

Student Grade

A 100

B 100

C 80

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

Page 16: 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

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

Page 17: 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

17

Questions?