<<

2/7/2018

Lectures 7: Introduction to SQL 6 3. Advanced SQL‐izing Lecture and activity contents are based on what Prof Chris Ré used in his CS 145 in the fall 2016 term with permission.

2

What you will learn about in this section Quantifiers

1. Quantifiers Product(name, price, company) Company(name, city)

2. NULLs SELECT DISTINCT Company.cname Find all companies FROM Company, Product that make some 3. Outer Joins WHERE Company.cname = Product.manuf products with price AND Product.price < 100 < 100

4. ACTIVITY: Fancy SQL Pt. II An existential quantifier is a logical quantifier (roughly) Existential: easy !  of the form “there exists”

3 4

Quantifiers NULLS in SQL Find all companies • Whenever we don’t have a value, we can put a NULL Product(name, price, company) with products all Company(name, city) price < 100 • Can mean many things: • Value does not exists Equivalent SELECT DISTINCT Company.cname • Value exists but is unknown FROM Company • Value not applicable Find all companies WHERE Company.cname NOT IN( • Etc. SELECT Product.manuf that make only FROM Product products with price WHERE Product.price >= 100) < 100 • The schema specifies for each attribute if can be null (nullable attribute) or not A universal quantifier is of the form “for all” Universal: hard !  • How does SQL cope with tables that have NULLs? 5 6

1 2/7/2018

Null Values Null Values

• For numerical operations, NULL ‐> NULL: • C1 AND C2 = min(C1, C2) • If x = NULL then 4*(3‐x)/7 is still NULL • C1 OR C2 = max(C1, C2) • NOT C1 = 1 – C1 • For boolean operations, in SQL there are three values: SELECT * Won’t return e.g. FROM Person FALSE = 0 (age=20 WHERE (age < 25) height=NULL UNKNOWN = 0.5 AND (height > 6 AND weight > 190) weight=200)! TRUE = 1

• If x= NULL then the comparison x=“Joe” results in UNKNOWN Rule in SQL: include only tuples that yield TRUE (1.0)

7 8

Null Values Null Values

Unexpected behavior: Can test for NULL explicitly: • x IS NULL • x IS NOT NULL SELECT * FROM Person WHERE age < 25 OR age >= 25 SELECT * FROM Person WHERE age < 25 OR age >= 25 Some Persons are not included ! OR age IS NULL

Now it includes all Persons!

9 10

RECAP: Inner Joins Inner Joins + NULLS = Lost data?

By default, joins in SQL are “inner joins”: By default, joins in SQL are “inner joins”:

Product(name, category) Product(name, category) Purchase(prodName, store) Purchase(prodName, store)

SELECT Product.name, Purchase.store SELECT Product.name, Purchase.store FROM Product FROM Product JOIN Purchase ON Product.name = Purchase.prodName JOIN Purchase ON Product.name = Purchase.prodName Both equivalent: SELECT Product.name, Purchase.store Both INNER JOINS! SELECT Product.name, Purchase.store FROM Product, Purchase FROM Product, Purchase WHERE Product.name = Purchase.prodName WHERE Product.name = Purchase.prodName

However: Products that never sold (with no Purchase tuple) will be lost!

11 12

2 2/7/2018

Outer Joins INNER JOIN: Product Purchase

• An outer returns tuples the joined relations that don’t have a name category prodName store corresponding tuple in the other relations Gizmo gadget Gizmo Wiz • 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… Camera Photo Camera Ritz • A LEFT OUTER JOIN will return a tuple (a, NULL)! OneClick Photo Camera Wiz

• Left outer joins in SQL: SELECT Product.name, Purchase.store name store FROM Product SELECT Product.name, Purchase.store LEFT OUTER JOIN Purchase ON FROM Product Gizmo Wiz Product.name = Purchase.prodName INNER JOIN Purchase ON Product.name = Purchase.prodName Camera Ritz Camera Wiz Now we’ll get products even if they didn’t sell Note: another equivalent way to write an INNER JOIN! 13 14

LEFT OUTER JOIN: Other Outer Joins Product Purchase

name category prodName store

Gizmo gadget Gizmo Wiz • Left outer join:

Camera Photo Camera Ritz • Include the left tuple even if there’s no match

OneClick Photo Camera Wiz • Right outer join: name store • Include the right tuple even if there’s no match

SELECT Product.name, Purchase.store Gizmo Wiz FROM Product Camera Ritz • LEFT OUTER JOIN Purchase Full outer join: • ON Product.name = Purchase.prodName Camera Wiz Include the both left and right tuples even if there’s no match

OneClick NULL 15 16

Lecture 2 & 3 > SUMMARY Summary

SQL is a rich programming language that handles the way data is processed Activity‐3‐3.ipynb declaratively

17 18

3