Lecture 4 SQL Adv II PDF
Lecture 4 SQL Adv II PDF
4:
Advanced SQL – Part II
Lecture 4
Announcements!
2
Lecture 4
Lecture 4:
Advanced SQL – Part II
Lecture 4
Today’s Lecture
2. Advance SQL-izing
• ACTIVITY: Fancy SQL Part II
4
Lecture 4 > Section 1
5
Lecture 4 > Section 1
2. GROUP BY
6
Lecture 4 > Section 1 > Aggregation
Aggregation
SELECT AVG(price) SELECT COUNT(*)
FROM Product FROM Product
WHERE maker = “Toyota” WHERE year > 1995
7
Lecture 4 > Section 1 > Aggregation
Aggregation: COUNT
We probably want:
More Examples
Purchase(product, date, price, quantity)
9
Lecture 4 > Section 1 > Aggregation
Simple Aggregations
Purchase
Product Date Price Quantity
bagel 10/21 1 20
banana 10/3 0.5 10
banana 10/10 1 10
bagel 10/25 1.50 20
12
Lecture 4 > Section 1 > GROUP BY
13
Lecture 4 > Section 1 > GROUP BY
14
Lecture 4 > Section 1 > GROUP BY
15
Lecture 3 > Section 2 > GROUP BY
HAVING Clause
Why?
• S = Can ONLY contain attributes a1,…,ak and/or aggregates over other attributes
• C1 = is any condition on the attributes in R1,…,Rn
• C2 = is any condition on the aggregate expressions
18
Lecture 4 > Section 1 > GROUP BY
Evaluation steps:
1. Evaluate FROM-WHERE: apply condition C1 on the
attributes in R1,…,Rn
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
19
Lecture 4 > Section 1 > GROUP BY
21
Lecture 4 > Section 1 > GROUP BY
• Attempt #2- With group-by: How about when written this way?
Activity-4-1.ipynb
23
Lecture 4 > Section 2
3. Advanced SQL-izing
24
Lecture 4 > Section 2
2. NULLs
3. Outer Joins
25
Lecture 4 > Section 2 > Quantifiers
Quantifiers
Product(name, price, company)
Company(name, city)
An existential quantifier is a
logical quantifier (roughly) Existential: easy ! J
of the form “there exists”
26
Lecture 4 > Section 2 > Quantifiers
Quantifiers
Find all companies
Product(name, price, company) with products all
Company(name, city) having price < 100
Equivalent
SELECT DISTINCT Company.cname
FROM Company
WHERE Company.name NOT IN( Find all companies
SELECT Product.company that make only
FROM Product.price >= 100) products with price
< 100
A universal quantifier is of
the form “for all” Universal: hard ! L
27
Lecture 4 > Section 2 > NULLs
NULLS in SQL
• Whenever we don’t have a value, we can put a NULL
• The schema specifies for each attribute if can be null (nullable attribute) or
not
Null Values
• For numerical operations, NULL -> NULL:
• If x = NULL then 4*(3-x)/7 is still NULL
FALSE = 0
UNKNOWN = 0.5
TRUE = 1
29
Lecture 4 > Section 2 > NULLs
Null Values
• C1 AND C2 = min(C1, C2)
• C1 OR C2 = max(C1, C2)
• NOT C1 = 1 – C1
Null Values
Unexpected behavior:
SELECT *
FROM Person
WHERE age < 25 OR age >= 25
31
Lecture 4 > Section 2 > NULLs
Null Values
Can test for NULL explicitly:
• x IS NULL
• x IS NOT NULL
SELECT *
FROM Person
WHERE age < 25 OR age >= 25
OR age IS NULL
32
Lecture 4 > Section 2 > NULLs
33
Lecture 4 > Section 2 > NULLs
However: Products that never sold (with no Purchase tuple) will be lost!
34
Lecture 4 > Section 2 > Outer Joins
Outer Joins
• An outer join returns tuples from the joined relations that don’t have a
corresponding tuple in the other relations
• I.e. If we join relations A and B on a.X = b.X, and there is an entry in A with X=5, but
none in B with X=5…
• A LEFT OUTER JOIN will return a tuple (a, NULL)!
INNER JOIN:
Product Purchase
name category prodName store
name store
SELECT Product.name, Purchase.store
FROM Product Gizmo Wiz
INNER JOIN Purchase
ON Product.name = Purchase.prodName Camera Ritz
Camera Wiz
Note: another equivalent way to write an
INNER JOIN!
36
Lecture 4 > Section 2 > Outer Joins
name store
OneClick NULL 37
Lecture 4 > Section 2 > Outer Joins
38
Lecture 3 > Section 3 > ACTIVITY
Activity-4-2.ipynb
39
Lecture 2, 3, & 4 > SUMMARY
Summary
40
Lecture 4 > Problem Set #1
Problems in PS#1
1. Linear algebra in SQL
42
Lecture 4 > Problem Set #1
43
Lecture 4 > Problem Set #1
44
Lecture 4 > Problem Set #1
45
Lecture 4 > Problem Set #1
1. Views: Details in the description. Nothing more than temp aliases for
queries. Remember: SQL is compositional!
46
Lecture 4 > Problem Set #1
47