Documente Academic
Documente Profesional
Documente Cultură
Outline
Unions, intersections, differences
(6.2.5, 6.4.2)
Subqueries (6.3)
Aggregations (6.4.3 6.4.6)
Hint for reading the textbook: read the entire chapter 6 !
Recommended reading from SQL for Nerds: chapter 4, More
complex queries (you will find it very useful for subqueries)
2
Cartesian Product
SELECT
SELECT **
FROM
FROM Games
Games
Game
Color
Baketball
Footbal
Tennis
SELECT
SELECT **
FROM
FROM Colors
Colors
Color_ID
Color_Nam
e
RED
GREEN
Cartesian Product
SELECT
SELECT **
FROM
FROM Games,
Games,Colors
Colors
Game
Color
Color_ID
Color_Name
Baketball
RED
Baketball
GREEN
Footbal
RED
Footbal
GREEN
Tennis
RED
Tennis
GREEN
4
Cartesian Product
SELECT
SELECT **
FROM
FROM Games,
Games,Colors
Colors
WHERE
WHEREColor
Color==Color_ID
Color_ID
Game
Color
Color_ID
Color_Name
Baketball
RED
Baketball
GREEN
Footbal
RED
Footbal
GREEN
Tennis
RED
Tennis
GREEN
5
Cartesian Product
SELECT
SELECT **
FROM
FROM Games,
Games,Colors
Colors
WHERE
WHEREColor
Color==Color_ID
Color_ID
Game
Color
Color_ID
Color_Name
Baketball
RED
Footbal
GREEN
Tennis
RED
Renaming Columns
Product
SELECT
SELECT
FROM
FROM
WHERE
WHERE
PName
Price
Category
Manufacturer
Gizmo
$19.99
Gadgets
GizmoWorks
Powergizmo
$29.99
Gadgets
GizmoWorks
SingleTouch
$149.99
Photography
Canon
MultiTouch
$203.99
Household
Hitachi
Pname
PnameAS
ASprodName,
prodName,Price
PriceAS
ASaskPrice
askPrice
Product
Product
Price
Price>>100
100
Query with
renaming
prodName
askPrice
SingleTouch
$149.99
MultiTouch
$203.99
7
Looking for R (S U T)
But what happens if T is empty?
9
((SELECT
SELECTR.A
R.AFROM
FROMRR))
INTERSECT
INTERSECT
((
((SELECT
SELECTS.A
S.AFROM
FROMSS))
UNION
UNION
((SELECT
SELECTT.A
T.AFROM
FROMTT))
))
10
Conserving Duplicates
(SELECT
(SELECT name
name
FROM
FROM Person
Person
WHERE
WHERE City=Seattle)
City=Seattle)
UNION
UNION ALL
ALL
(SELECT
(SELECT name
name
FROM
FROM Person,
Person,Purchase
Purchase
WHERE
WHERE buyer=name
buyer=nameAND
ANDstore=The
store=TheBon)
Bon)
11
Subqueries (Static..)
A subquery producing a single value:
SELECT
SELECTPurchase.product
Purchase.product
FROM
FROM Purchase
Purchase
WHERE
WHERE buyer
buyer==
(SELECT
(SELECT name
name
FROM
FROM Person
Person
WHERE
WHERE ssn
ssn==123456789);
123456789);
Removing Duplicates
SELECT
SELECT Company.name
Company.name
FROM
FROM Company,
Company,Product,
Product,Purchase
Purchase
WHERE
WHERE Company.name=
Company.name=Product.maker
Product.maker
AND
AND Product.name
Product.name ==Purchase.product
Purchase.product
AND
ANDPurchase.buyer
Purchase.buyer==Joe
JoeBlow
Blow
Multiple copies
SELECT
SELECTDISTINCT
DISTINCTCompany.name
Company.name
FROM
FROM Company,
Company,Product,
Product,Purchase
Purchase
WHERE
WHERE Company.name=
Company.name=Product.maker
Product.maker
AND
AND Product.name
Product.name ==Purchase.product
Purchase.product
AND
ANDPurchase.buyer
Purchase.buyer==Joe
JoeBlow
Blow
Single copies
16
Removing Duplicates
SELECT
SELECTDISTINCT
DISTINCTCompany.name
Company.name
FROM
FROM Company,
Company,Product
Product
WHERE
WHERE Company.name=
Company.name=Product.maker
Product.maker
AND
AND Product.name
Product.name IN
IN
(SELECT
(SELECTPurchase.product
Purchase.product
FROM
FROM Purchase
Purchase
WHERE
WHEREPurchase.buyer
Purchase.buyer==Joe
JoeBlow)
Blow)
SELECT
SELECTDISTINCT
DISTINCTCompany.name
Company.name
FROM
FROM Company,
Company,Product,
Product,Purchase
Purchase
WHERE
WHERE Company.name=
Company.name=Product.maker
Product.maker
AND
AND Product.name
Product.name ==Purchase.product
Purchase.product
AND
AND Purchase.buyer
Purchase.buyer==Joe
JoeBlow
Blow 17
Now
they are
equivalent
19
Conditions on Tuples
SELECT
SELECT DISTINCT
DISTINCTCompany.name
Company.name
FROM
FROM Company,
Company,Product
Product
WHERE
WHERE Company.name=
Company.name=Product.maker
Product.maker
AND
AND (Product.name,price)
(Product.name,price) IN
IN
(SELECT
(SELECTPurchase.product,
Purchase.product,Purchase.price)
Purchase.price)
FROM
FROM Purchase
Purchase
WHERE
WHEREPurchase.buyer
Purchase.buyer==Joe
JoeBlow);
Blow);
May not work in MySQL...
20
Correlated Queries
Movie (title, year, director, length)
Find movies whose title appears more than once.
correlation
SELECT
SELECTDISTINCT
DISTINCTtitle
title
FROM
FROM Movie
MovieAS
ASxx
WHERE
WHERE year
year<>
<>ANY
ANY
(SELECT
(SELECT year
year
FROM
FROM Movie
Movie
WHERE
WHERE title
title== x.title);
x.title);
Note (1) scope of variables (2) this can still be expressed as single SFW
21
Aggregation
SELECT
SELECT
FROM
FROM
WHERE
WHERE
AVG(price)
AVG(price)
Product
Product
maker=Toyota
maker=Toyota
23
Aggregation: Count
SELECT
SELECT COUNT(*)
COUNT(*)
FROM
FROM Product
Product
WHERE
WHERE year
year>>1995
1995
Aggregation: Count
COUNT applies to duplicates, unless otherwise stated:
SELECT Count(category)
FROM Product
WHERE year > 1995
same as Count(*)
Better:
SELECT Count(DISTINCT category)
FROM Product
WHERE year > 1995
25
Simple Aggregation
Purchase(product, date, price, quantity)
Example 1: find total sales for the entire database
SELECT Sum(price * quantity)
FROM
Purchase
Example 1: find total sales of bagels
SELECT Sum(price * quantity)
FROM
Purchase
WHERE product = bagel
26
Simple
Aggregations
Purchase
27
28
29
Date
Price
Quantity
Banana
10/19
0.52
17
Banana
10/22
0.52
Bagel
10/20
0.85
20
Bagel
10/21
0.85
15
30
Then, aggregate
Product
TotalSales
Bagel
$29.75
Banana
$12.48
SELECT
SELECT product,
product,Sum(price*quantity)
Sum(price*quantity)AS
ASTotalSales
TotalSales
FROM
Purchase
FROM
Purchase
WHERE
date
WHERE
date>>10/1
10/1
GROUPBY
GROUPBY product
product
31
SELECT
SELECTDISTINCT
DISTINCT x.product,
x.product,(SELECT
(SELECTSum(y.price*y.quantity)
Sum(y.price*y.quantity)
FROM
FROM Purchase
Purchaseyy
WHERE
WHEREx.product
x.product==y.product
y.product
AND
ANDy.date
y.date>>10/1)
10/1)
AS
ASTotalSales
TotalSales
FROM
Purchase
FROM
Purchasexx
WHERE
32
WHERE x.date
x.date>>10/1
10/1
Another Example
For every product, what is the total sales and max quantity sold?
SELECT
product,
SELECT
product,Sum(price
Sum(price**quantity)
quantity)AS
ASSumSales
SumSales
Max(quantity)
Max(quantity)AS
ASMaxQuantity
MaxQuantity
FROM
Purchase
FROM
Purchase
GROUP
GROUPBY
BYproduct
product
Product
SumSales
MaxQuantity
Banana
$12.48
17
Bagel
$29.75
20
33
HAVING Clause
Same query, except that we consider only products that had
at least 100 buyers.
SELECT
product,
SELECT
product,Sum(price
Sum(price**quantity)
quantity)
FROM
Purchase
FROM
Purchase
WHERE
date
WHERE
date>>9/1
9/1
GROUP
GROUPBY
BYproduct
product
HAVING
HAVING Sum(quantity)
Sum(quantity)>>100
100
HAVING clause contains conditions on aggregates.
34
Why ?
35
3.
4.
Aggregation
Author(login,name)
Document(url, title)
Wrote(login,url)
Mentions(url,word)
37
This is
SQL by
a novice
SELECT
SELECTDISTINCT
DISTINCTAuthor.name
Author.name
FROM
Author
FROM
Author
WHERE
count(SELECT
WHERE
count(SELECTWrote.url
Wrote.url
FROM
FROMWrote
Wrote
WHERE
WHEREAuthor.login=Wrote.login)
Author.login=Wrote.login)
>>10
10
38
This is
SQL by
an expert
39
No need for DISTINCT: automatically from GROUP
BY