Lec03 SQL Joins
Lec03 SQL Joins
1
SQL Joins
• Can use data from multiple tables:
2
Interpreting Joins
• A JOIN B produces one row for every pair of rows
– one row from A and one row from B
3
Interpreting Joins
• A JOIN B produces one row for every pair of rows
– one row from A and one row from B
4
Interpreting Joins
• A JOIN B produces one row for every pair of rows
– one row from A and one row from B
5
Interpreting Joins
• A JOIN B produces one row for every pair of rows
– one row from A and one row from B
6
Interpreting Joins
• A JOIN B produces one row for every pair of rows
– one row from A and one row from B
7
Interpreting Joins
• A JOIN B produces one row for every pair of rows
– one row from A and one row from B
8
Interpreting Joins
• A JOIN B produces one row for every pair of rows
– one row from A and one row from B
Cname Country Pname Price Manufacturer
Canon Japan SingleTouch 149.99 Canon
11
(Inner) Joins
SELECT a1, a2, …, an
FROM R1, R2, …, Rm
WHERE Cond
12
(Inner) joins
Company(cname, country)
Product(pname, price, category, manufacturer)
– manufacturer is foreign key
13
(Inner) joins
SELECT DISTINCT cname
FROMProduct, Company
WHERE country = ‘USA’ AND category = ‘gadget’
AND manufacturer = cname
Product Company
pname category manufacturer cname country
14
(Inner) joins
SELECT DISTINCT cname
FROMProduct, Company
WHERE country = ‘USA’ AND category = ‘gadget’
AND manufacturer = cname
Product Company
pname category manufacturer cname country
15
(Inner) joins
SELECT DISTINCT cname
FROMProduct, 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
Canon Japan
Hitachi Japan
Alternative syntax:
19
Name Conflicts we used cname / pname
to avoid this problem
27
Self-joins
SELECT DISTINCT z.cname
FROMProduct 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
28
Self-joins
SELECT DISTINCT z.cname
FROMProduct 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
29
Self-joins
SELECT DISTINCT z.cname
FROMProduct 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
30
Self-joins
SELECT DISTINCT z.cname
FROMProduct 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
Gizmo gadget GizmoWorks GizmoWorks USA
y SingleTouch photo Hitachi Hitachi Japan
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
36
SELECT Product.name, Purchase.store
FROM Product JOIN Purchase ON
Product.name = Purchase.prodName
Product Purchase
Name Category ProdName Store
37
SELECT Product.name, Purchase.store
FROM Product JOIN Purchase ON
Product.name = Purchase.prodName
Product Purchase
Name Category ProdName Store
Name Store
Gizmo Wiz
38
SELECT Product.name, Purchase.store
FROM Product JOIN Purchase ON
Product.name = Purchase.prodName
Product Purchase
Name Category ProdName Store
Name Store
Gizmo Wiz
39
SELECT Product.name, Purchase.store
FROM Product JOIN Purchase ON
Product.name = Purchase.prodName
Product Purchase
Name Category ProdName Store
Name Store
Gizmo Wiz
40
SELECT Product.name, Purchase.store
FROM Product JOIN Purchase ON
Product.name = Purchase.prodName
Product Purchase
Name Category ProdName Store
Name Store
Gizmo Wiz
41
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
42
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
43
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
44
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
45
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
46
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
47
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
48
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
49
Join Examples
• See lec03-sql-joins.sql…
50