Top Banner
M.P. Johnson, DBMS, Stern/NYU, Sprin g 2008 1 C20.0046: Database Management Systems Lecture #9 M.P. Johnson Stern School of Business, NYU Spring, 2008
31

C20.0046: Database Management Systems Lecture #9

Feb 22, 2016

Download

Documents

lanza

C20.0046: Database Management Systems Lecture #9. M.P. Johnson Stern School of Business, NYU Spring, 2008. Agenda. Subqueries, etc. Sets, etc. From last time: escaping single quotes. http://reddit.com/info/69jzv/comments/. Subqueries. Subquery: copy in Conrad’s selection for his ssn: - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

1

C20.0046: Database Management SystemsLecture #9

M.P. JohnsonStern School of Business, NYUSpring, 2008

Page 2: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

2

Agenda Subqueries, etc.

Sets, etc.

Page 3: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

3

From last time: escaping single quotes http://reddit.com/info/69jzv/comments/

Page 4: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

4

Subqueries Subquery: copy in Conrad’s selection for his ssn:

The subquery returns one value, so the = is valid If it returns more (or fewer), we get a run-time error

SELECT Purchase.prodnameFROM PurchaseWHERE buyerssn = (SELECT ssn FROM Person WHERE name = 'Christo')

Page 5: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

5

Operators on subqueries Several new operators applied to (unary)

selections:1. IN R2. EXISTS R3. UNIQUE R4. s > ALL R5. s > ANY R6. x IN R

> is just an example op Each expression can be negated with NOT

Page 6: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

6

Next: ALL opEmployees(name, job, divid, salary)Find which employees are paid more than all the programmers

SELECT nameFROM EmployeesWHERE salary > ALL (SELECT salary FROM Employees WHERE job='programmer')

Page 7: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

7

ANY/SOME opEmployees(name, job, divid, salary)Find which employees are paid more than at least one vice president

SELECT nameFROM EmployeesWHERE salary > ANY (SELECT salary FROM Employees WHERE job='VP')

Page 8: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

8

ANY/SOME opEmployees(name, job, divid, salary)Find which employees are paid more than at least one vice president

SELECT nameFROM EmployeesWHERE salary > SOME (SELECT salary FROM Employees WHERE job='VP')

Page 9: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

9

Existential/Universal ConditionsEmployees(name, job, divid, salary)

Division(name, id, head)

Find all divisions with an employee whose salary is > 100000

Existential: easy!

SELECT DISTINCT Division.nameFROM Employees, DivisionWHERE salary > 100000 AND divid=id

Page 10: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

10

Existential/Universal ConditionsEmployees(name, job, divid, salary)

Division(name, id, head)

Find all divisions in which everyone makes > 100000

Existential: easy!

Page 11: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

11

Existential/universal with IN

2. Select the divisions we didn’t find:

1. Find the other divisions: in which someone makes <= 100000:

SELECT nameFROM DivisionWHERE id IN (SELECT divid FROM Employees WHERE salary <= 100000

SELECT nameFROM DivisionWHERE id NOT IN (SELECT divid FROM Employees WHERE salary <= 100000

Page 12: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

12

Next: correlated subqueries Acc(name,bal,type…) Q: Who has the largest balance?

Can we do this with subqueries?

Page 13: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

13

Acc(name,bal,type,…) Q: Find holder of largest account

SELECT nameFROM AccWHERE bal >= ALL (SELECT bal FROM Acc)

Correlated Queries

Page 14: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

14

Correlated Queries So far, subquery executed once;

result used for higher query More complicated: correlated queries

“[T]he subquery… [is] evaluated many times, once for each assignment of a value to some term in the subquery that comes from a tuple variable outside the subquery” (Ullman, p286).

Q: What does this mean? A: That subqueries refer to vars from outer queries

Page 15: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

15

Acc(name,bal,type,…) Q2: Find holder of largest account of each type

SELECT name, typeFROM AccWHERE bal >= ALL (SELECT bal FROM Acc WHERE type=type)

Correlated Queries

correlation

Page 16: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

16

Acc(name,bal,type,…) Q2: Find holder of largest account of each type

Note:1. scope of variables2. this can still be expressed as single SFW

SELECT name, typeFROM Acc a1WHERE bal >= ALL (SELECT bal FROM Acc WHERE type=a1.type)

Correlated Queries

correlation

Page 17: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

17

New topic: R.A./SQL Set Operators Relations are sets have set-theoretic ops

Venn diagrams

Union: R1 R2 Example:

ActiveEmployees RetiredEmployees

Difference: R1 – R2 Example:

AllEmployees – RetiredEmployees = ActiveEmployees

Intersection: R1 R2 Example:

RetiredEmployees UnionizedEmployees

Page 18: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

18

Set operations - exampleName Address Gender BirthdateFisher 123 Maple F 9/9/99Hamill 456 Oak M 8/8/88

Name Address Gender BirthdateFisher 123 Maple F 9/9/99Ford 345 Palm M 7/7/77

R:

S:

Name Address Gender BirthdateFisher 123 Maple F 9/9/99Hamill 456 Oak M 8/8/88Ford 345 Palm M 7/7/77

R S:

Page 19: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

19

Set operations - exampleName Address Gender BirthdateFisher 123 Maple F 9/9/99Hamill 456 Oak M 8/8/88

Name Address Gender BirthdateFisher 123 Maple F 9/9/99Ford 345 Palm M 7/7/77

R:

S:

R S: Name Address Gender BirthdateFisher 123 Maple F 9/9/99

Page 20: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

20

Set operations - exampleName Address Gender BirthdateFisher 123 Maple F 9/9/99Hamill 456 Oak M 8/8/88

Name Address Gender BirthdateFisher 123 Maple F 9/9/99Ford 345 Palm M 7/7/77

R:

S:

R - S: Name Address Gender BirthdateHamill 456 Oak M 8/8/88

Page 21: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

21

Set ops in SQL Orthodox SQL has set operators:

UNION, INTERSECT, EXCEPT Oracle SQL uses MINUS rather than EXCEPT See the Ullman page on more differences

These ops applied to queries:

(SELECT name FROM Person WHERE City = 'New York')

INTERSECT(SELECT custname FROM Purchase WHERE store='Kim''s')

Page 22: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

22

Boat examples Reserve(ssn,bmodel,color)

Q: Find ssns of sailors who reserved red boats or green boats

SELECT DISTINCT ssnFROM reserveWHERE color = 'red' OR color = 'green'

Page 23: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

23

Boat examples Reserve(ssn,bmodel,color)

Q: Find ssns of sailors who reserved red boats and green boats

SELECT DISTINCT ssnFROM reserveWHERE color = 'red' AND color = 'green'

Page 24: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

24

Boat examples Reserve(ssn,bmodel,color)

Q: Find ssns of sailors who reserved red boats and green boats

SELECT DISTINCT r1.ssnFROM reserve r1, reserve r2WHERE r1.ssn = r2.ssn AND r1.color = 'red' AND r2.color = 'green'

Page 25: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

25

Boat examples Reserve(ssn,bmodel,color)

Q: Find ssns of sailors who reserved red boats and green boats

(SELECT DISTINCT ssn FROM reserve WHERE color = 'red') INTERSECT(SELECT DISTINCT ssn

FROM reserve WHERE color = 'green')

Page 26: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

26

Boat examples Reserve(ssn,bmodel,color)

Q: Find ssns of sailors who reserved red boats or green boats

(SELECT DISTINCT ssn FROM reserve WHERE color = 'red') UNION (SELECT DISTINCT ssn

FROM reserve WHERE color = 'green')

Page 27: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

27

Boat examples Reserve(ssn,bmodel,color)

Q: Find ssns of sailors who reserved red boats but not green boats

(SELECT DISTINCT ssn FROM reserve WHERE color = 'red') EXCEPT (SELECT DISTINCT ssn

FROM reserve WHERE color = 'green')

Page 28: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

28

(SELECT name, address FROM Cust1)

UNION(SELECT name FROM Cust2)

Union-Compatibility Situation: Cust1(name,address,…), Cust2(name,…) Want: report of all customer names and addresses

(if known) Can’t do:

Both tables must have same sequence of types Applies to all set ops

Page 29: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

29

Union-Compatibility Situation: Cust1(name,address,…), Cust2(name,…) Want: report of all customer names and addresses

(if known) But can do:

Resulting field names taken from first table

(SELECT name, address FROM Cust1)

UNION(SELECT name, '(N/A)' FROM Cust2)

Result(name, address)

Page 30: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

30

First Unintuitive SQLism Looking for R (S T)

But what happens if T is empty?

See transcript of this in Oracle on sales

SELECT R.AFROM R, S, TWHERE R.A=S.A OR R.A=T.A

Page 31: C20.0046: Database Management Systems Lecture #9

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

31

Review Examples from sqlzoo.net

SELECT LFROM R1, …, Rn

WHERE C

L(C(R1 x … Rn)