Lec04 SQL Aggregates
Lec04 SQL Aggregates
1
Outline
• Inner joins (6.2, review)
• Outer joins (6.3.8)
• Aggregations (6.4.3 – 6.4.6)
2
UNIQUE
• PRIMARY KEY adds implicit “NOT NULL” constraint
while UNIQUE does not
– you would have to add this explicitly for UNIQUE:
3
(Inner) Joins
SELECT a1, a2, …, an
FROM R1, R2, …, Rm
WHERE Cond
4
(Inner) joins
Company(cname, country)
Product(pname, price, category, manufacturer)
– manufacturer is foreign key
5
(Inner) joins
SELECT DISTINCT cname
FROM Product, Company
WHERE country = ‘USA’ AND category = ‘gadget’AND
manufacturer = cname
Product Company
pname category manufacturer cname country
6
(Inner) joins
SELECT DISTINCT cname
FROM Product, Company
WHERE country = ‘USA’ AND category = ‘gadget’AND
manufacturer = cname
Product Company
pname category manufacturer cname country
Product Company
pname category manufacturer cname country
Product Company
pname category manufacturer cname country
Product Company
pname category manufacturer cname country
Product Company
pname category manufacturer cname country
Product Company
pname category manufacturer cname country
Product Company
pname category manufacturer cname country
Product Company
pname category manufacturer cname country
Canon Japan
Hitachi Japan
Alternative syntax:
19
Name Conflicts we used cname / pname
to avoid this problem
19
Self-joins
SELECT DISTINCT z.cname
FROM Product x, Product y, Company z
WHERE z.country = ‘USA’
AND x.category = ‘gadget’
AND y.category = ‘photo’
AND x.manufacturer = cname
AND y.manufacturer = cname;
Product Company
pname category manufacturer cname country
20
Self-joins
SELECT DISTINCT z.cname
FROM Product x, Product y, Company z
WHERE z.country = ‘USA’
AND x.category = ‘gadget’
AND y.category = ‘photo’
AND x.manufacturer = cname
AND y.manufacturer = cname;
Product Company
x pname category manufacturer cname country
21
Self-joins
SELECT DISTINCT z.cname
FROM Product x, Product y, Company z
WHERE z.country = ‘USA’
AND x.category = ‘gadget’
AND y.category = ‘photo’
AND x.manufacturer = cname
AND y.manufacturer = cname;
Product Company
x pname category manufacturer cname country
y Gizmo gadget GizmoWorks GizmoWorks USA
22
Self-joins
SELECT DISTINCT z.cname
FROM Product x, Product y, Company z
WHERE z.country = ‘USA’
AND x.category = ‘gadget’
AND y.category = ‘photo’
AND x.manufacturer = cname
AND y.manufacturer = cname;
Product Company
x pname category manufacturer cname country z
y Gizmo gadget GizmoWorks GizmoWorks USA
Or equivalently:
SELECT Product.name, …, Purchase.store
FROM Product JOIN Purchase ON
Product.name = Purchase.prodName
28
SELECT Product.name, Purchase.store
FROM Product JOIN Purchase ON
Product.name = Purchase.prodName
Product Purchase
Name Category ProdName Store
28
SELECT Product.name, Purchase.store
FROM Product JOIN Purchase ON
Product.name = Purchase.prodName
Product Purchase
Name Category ProdName Store
29
SELECT Product.name, Purchase.store
FROM Product JOIN Purchase ON
Product.name = Purchase.prodName
Product Purchase
Name Category ProdName Store
Name Store
Gizmo Wiz
30
SELECT Product.name, Purchase.store
FROM Product JOIN Purchase ON
Product.name = Purchase.prodName
Product Purchase
Name Category ProdName Store
Name Store
Gizmo Wiz
31
SELECT Product.name, Purchase.store
FROM Product JOIN Purchase ON
Product.name = Purchase.prodName
Product Purchase
Name Category ProdName Store
Name Store
Gizmo Wiz
32
SELECT Product.name, Purchase.store
FROM Product JOIN Purchase ON
Product.name = Purchase.prodName
Product Purchase
Name Category ProdName Store
Name Store
Gizmo Wiz
33
SELECT Product.name, Purchase.store
FROM Product JOIN Purchase ON
Product.name = Purchase.prodName
Product Purchase
Name Category ProdName Store
Name Store
Gizmo Wiz
Camera Ritz
34
SELECT Product.name, Purchase.store
FROM Product JOIN Purchase ON
Product.name = Purchase.prodName
Product Purchase
Name Category ProdName Store
Name Store
Gizmo Wiz
Camera Ritz
Camera Wiz
35
SELECT Product.name, Purchase.store
FROM Product JOIN Purchase ON
Product.name = Purchase.prodName
Product Purchase
Name Category ProdName Store
Name Store
Gizmo Wiz
Camera Ritz
Camera Wiz
36
SELECT Product.name, Purchase.store
FROM Product LEFT OUTER JOIN Purchase ON
Product.name = Purchase.prodName
Product Purchase
Name Category ProdName Store
Name Store
Gizmo Wiz
Camera Ritz
Camera Wiz
37
SELECT Product.name, Purchase.store
FROM Product LEFT OUTER JOIN Purchase ON
Product.name = Purchase.prodName
Product Purchase
Name Category ProdName Store
Name Store
Gizmo Wiz
Camera Ritz
Camera Wiz
OneClick NULL
38
SELECT Product.name, Purchase.store
FROM Product RIGHT OUTER JOIN Purchase ON
Product.name = Purchase.prodName
Product Purchase
Name Category
ProdName Store
Gizmo gadget
Gizmo Wiz
Camera Photo
Camera Ritz
OneClick Photo
Camera Wiz
Gizmo Wiz
Camera Ritz
Camera Wiz
NULL Foo
39
SELECT Product.name, Purchase.store
FROM Product FULL OUTER JOIN Purchase ON
Product.name = Purchase.prodName
Product Purchase
Name Category
ProdName Store
Gizmo gadget
Gizmo Wiz
Camera Photo
Camera Ritz
OneClick Photo
Camera Wiz
Gizmo Wiz
Camera Ritz
Camera Wiz
OneClick NULL
l
NULL Foo
40
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 both left and right tuples even if there’s no
match
41
Outer Joins Example
See lec04-sql-outer-joins.sql…
42
Aggregation in SQL
>sqlite3 lecture04
43
Comment about SQLite
• One cannot load NULL values such that they
are actually loaded as null values
select sum(quantity)
from Purchase
where quantity is not null; 46
Aggregates and NULL Values
Null values are not used in aggregates
insert into Purchase
values(12, 'gadget', NULL, NULL, 'april')
Let’s try the following
select count(*) from Purchase
-- NULL is counted in count(*)
select count(quantity) from Purchase
-- NULL is ignored in count(quantity)
select sum(quantity)
from Purchase
where quantity is not null;
-- “is not null” is redundant
47
Counting Duplicates
COUNT applies to duplicates, unless otherwise stated:
We probably want:
SELECT Count(DISTINCT product)
FROM Purchase
WHERE price> 4.99
48
More Examples
49
Simple Aggregations
Purchase Product Price Quantity
Bagel 3 20
Bagel 1.50 20
Banana 0.5 50
Banana 2 10
Banana 4 10
SELECT Sum(price * quantity)
FROM Purchase 90 (= 60+30)
WHERE product = ‘Bagel’
50
Simple Aggregations
Purchase Product Price Quantity
Bagel 3 20
Bagel 1.50 20
Banana 0.5 50
Banana 2 10
Banana 4 10
SELECT Sum(price * quantity)
FROM Purchase 90 (= 60+30)
WHERE product = ‘Bagel’
51
More Examples
How can we find the average revenue per sale?
52
More Examples
SELECT sum(price * quantity) / count(*)
FROM Purchase
WHERE product = ‘bagel’
53