Docsity

SQL 2 – The Sequel Review: SQL DML Nested Queries ..., Schemes and Mind Maps of Logic

Toggle sidebar
bg1
1
CS 186, Spring 2007, Lect ure 8
R&G, Chapter 5
Mary Roth
∀
∃
The important thing is not to
stop questioning.
Albert Einstein
Life is just a bowl of queries.
-Anon
(not Forrest Gump)
SQL 2 – The Sequel
Review: SQL DML
• DML includes 4 main statements:
SELECT (query), INSERT, UPDATE and DELE TE
• e.g: SELECT S.name, E.cid
FROM Students S, Enrolled E
WHERE S.sid = E.sid AND
S.age=19
SELECT
PROJECT
JOIN
SELECT R.sid
FROM Boats B,Reser ves R
WHERE B.color = ‘ red’
AND R.bid=B .bid
INTERSECT
SELECT R.sid
FROM Boats B,Reser ves R
WHERE B.color = ‘ green’ AND
R.bid=B.bid Reserves
9/131032
9/131051
9/121011
daybidsid
redSanta Maria103
greenTitanic105
bluePinta102
redNina101
colorbnamebid
Boats
2
1
sid
1
sid
∩=1
sid
Now let’s do this with
a self-join
Example: Find sailors who have reserved a r ed
and a green boat
SELECT R1.sid
FROM Boats B1, Res erves R1,
Boats B2, Reserv es R2
WHERE B1.color = ‘ red’
AND B1.bid=R1.bid
AND B2.color = ‘ green’
AND B2.bid=R2.bid
AND R1.sid=R2.sid
Find sids of sailo rs who’ve reserved a red an d a green
boat
9/131032
9/131051
9/121011
daybidsid
redSanta Maria103
greenTitanic105
bluePinta102
redNina101
colorbnamebid
Find red reserved boa ts
Find green reserved b oats
Find matching green a nd red
reserved boats
2
1
sid
1
sid
=1
sid
• The WHERE clause can itself contain an SQL query!
(Actually, so can FROM and HAVING clauses can too)
Nested Queries
SELECT S.sname
FROM Sailors S
WHERE S.sid IN (SE LECT R.sid
FROM Reserves R
WHERE R.bid=103)
e.g. Find the names of sailors who’ve reserved boat #103:
…and then check each the sid of
each tuple in Sailors to see if it is
in S1
First compute the set of all sailors that have
reserved boat 103…
9/131032
9/131051
9/121011
daybidsid
278Sam3
392Bilbo2
227Frodo1
ageratingsnamesid
Sailors
Reserves
S
2
sid
S
S
Bilbo
sname
• This nested q uery was
uncorrelated
because the subquery
does not refer to anything in the enclosing query
Nested Queries
SELECT S.sname
FROM Sailors S
WHERE S.sid IN (SE LECT R.sid
FROM Reserves R
WHERE R.bid=103)
It can evaluated once and the n checked for
each tuple in enclosing query
pf3
pf4
pf5

Partial preview of the text

Download SQL 2 – The Sequel Review: SQL DML Nested Queries ... and more Schemes and Mind Maps Logic in PDF only on Docsity!

CS 186, Spring 2007, Lecture 8 R&G, Chapter 5 Mary Roth

The important thing is not to stop questioning. Albert Einstein Life is just a bowl of queries. -Anon (not Forrest Gump)

SQL 2 – The Sequel

Review: SQL DML

  • DML includes 4 main statements:

SELECT (query), INSERT, UPDATE and DELETE

  • e.g: SELECT S.name, E.cid

FROM Students S, Enrolled E

WHERE S.sid = E.sid AND

S.age=

SELECT PROJECT JOIN SELECT R.sid FROM Boats B,Reserves R WHERE B.color = ‘red’ AND R.bid=B.bid INTERSECT SELECT R.sid FROM Boats B,Reserves R WHERE B.color = ‘green’ AND R.bid=B.bid Reserves 2 103 9/ 1 105 9/ 1 101 9/ sid bid day 103 Santa Maria red 105 Titanic green 102 Pinta blue 101 Nina red bid bname color Boats 2 1 sid 1 sid ∩ (^) = (^1) sid Now let’s do this with a self-join

Example: Find sailors who have reserved a red

and a green boat

SELECT R1.sid FROM Boats B1, Reserves R1, Boats B2, Reserves R WHERE B1.color = ‘red’ AND B1.bid=R1.bid AND B2.color = ‘green’ AND B2.bid=R2.bid AND R1.sid=R2.sid Find sids of sailors who’ve reserved a red and a green boat 2 103 9/ 1 105 9/ 1 101 9/ sidbid day 103 Santa Maria red 105 Titanic green 102 Pinta blue 101 Nina red bid bname color Find red reserved boats Find green reserved boats Find matching green and red reserved boats 2 1 sid 1 sid = (^1) sid

  • The WHERE clause can itself contain an SQL query! (Actually, so can FROM and HAVING clauses can too)

Nested Queries

SELECT S.sname FROM Sailors S WHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid=103) e.g. Find the names of sailors who’ve reserved boat #103: …and then check each the sid of each tuple in Sailors to see if it is in S First compute the set of all sailors that have reserved boat 103… 2 103 9/ 1 105 9/ 1 101 9/ sidbid day 3 Sam 8 27 2 Bilbo 2 39 1 Frodo 7 22 sidsname rating age Sailors Reserves S 2 sid S S Bilbo sname

  • This nested query was uncorrelated because the subquery does not refer to anything in the enclosing query

Nested Queries

SELECT S.sname FROM Sailors S WHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid=103) It can evaluated once and then checked for each tuple in enclosing query

  • Nested queries can also be correlated; the subquery refers to the enclosing query

Nested Queries with correlation

The subquery must be re-evaluated for each tuple in enclosing query SELECT S.sname FROM Sailors S WHERE EXISTS (SELECT * FROM Reserves R WHERE R.bid=103 AND R.sid=S.sid)  EXISTS is a set operator that is true if result of set expression has at least one tuple

Nested Queries

e.g. Find the names of sailors who’ve reserved boat #103: 2 103 9/ 1 105 9/ 1 101 9/ sidbid day 3 Sam 8 27 2 Bilbo 2 39 1 Frodo 7 22 sidsname rating age Sailors (^) Reserves S S S SELECT S.sname FROM Sailors S WHERE EXISTS (SELECT * FROM Reserves R WHERE R.bid=103 AND R.sid=S.sid) sidbid day Notice that this query computes the same answer as the previous query! 321 2 103 9/

Set-Comparison Operators

  • IN
    • True if is a member of
    • Also, NOT IN
  • EXISTS
    • True if evaluates to a set with at least one member
    • Also, NOT EXISTS
  • UNIQUE
    • True if evaluates to a set with no duplicates; each row can appear exactly once
    • Also, NOT UNIQUE
  • comparison op ANY
    • True if contains at least one member that makes the comparison true
    • Also, op ALL

Use NOT Exists for Division

SELECT S.sname FROM Sailors S WHERE NOT EXISTS (SELECT B.bid FROM Boats B WHERE NOT EXISTS (SELECT R.bid FROM Reserves R WHERE R.bid=B.bid AND R.sid=S.sid)) Find Sailors S such that ... there is no boat B... without a reservation by Sailor S

Find sailors who’ve reserved all boats.

X = set of sailors and Y = set of all boats with reservations. Recall: X/Y means only give me X tuples that have a match in Y. 2

Division

3 103 9/ 3 101 9/ 2 103 9/ 1 103 9/ 1 103 9/ sidbid day 3 Sam 8 27 2 Bilbo 2 39 1 Frodo 7 22 sidsname rating age Sailors Reserves S S S SELECT S.sname FROM Sailors S WHERE NOT EXISTS (SELECT B.bid FROM Boats B WHERE NOT EXISTS (SELECT R.bid FROM Reserves R WHERE R.bid=B.bid AND R.sid=S.sid)) 103 Pinta blue 101 Nina red bid bname color Boats 1 101 R R R B B 3 103 R R

UNIQUE

2 103 9/ 1 103 9/ 1 103 9/ sidbid day 3 Sam 8 27 2 Bilbo 2 39 1 Frodo 7 22 sidsname rating age Sailors (^) Reserves S S S sidbid day 2 103 9/ Find the names of sailors who’ve reserved boat # exactly once SELECT S.sname FROM Sailors S WHERE UNIQUE (SELECT sid, bid FROM Reserves R WHERE R.bid=103 AND S.sid=R.sid) (^321)

Queries With GROUP BY target-list contains:

  • list of column names from grouping–list
  • terms with aggregate operations (e.g., MIN ( S.age)). SELECT [DISTINCT] target-list FROM relation-list [WHERE qualification ] GROUP BY grouping-list
    • To generate values for a column based on groups of rows, use aggregate functions in SELECT statements with the GROUP BY clause First select these rows… Then group them by the values in these columns… And finally compute aggregate function over each group… Returning 1 row per group

Group By Example

For each rating, find the age of the youngest

sailor with age ≥ 18

SELECT S.rating, MIN (S.age) FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating 3 Sam 8 27 4 Pippin 2 21 5 Merry 8 17 2 Bilbo 2 39 1 Frodo 7 22 sidsname rating age Sailors Group 1 1 Frodo 7 22 4 Pippin 2 21 Group 2^2 Bilbo^239 Group 3 3 Sam 8 27 2 21 8 27 7 22 Find the number of reservations for each red boat. SELECT B.bid, COUNT(*) AS tot_res FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color=‘red’ GROUP BY B.bid 3 101 9/ 2 103 9/ 4 101 9/ 1 102 9/ sid bid day Reserves 103 Santa Maria red 102 Pinta blue 101 Nina red bid bname color Boats 3 101 9/ 4 101 9/ 2 103 9/ 103 1 101 2 Queries With GROUP BY and HAVING

  • Use the HAVING clause with the GROUP BY clause to restrict which group-rows are returned in the result set

SELECT [DISTINCT] target-list

FROM relation-list

WHERE qualification

GROUP BY grouping-list

HAVING group-qualification

Find the age of the youngest sailor with age ≥

18, for each rating with at least 2 such sailors

SELECT S.rating, MIN (S.age)

FROM Sailors S

WHERE S.age >= 18

GROUP BY S.rating

HAVING COUNT (*) > 1

Answer

Sailors who have reserved

all boats

3 Sam 8 27 2 Bilbo 2 39 1 Frodo 7 22 sidsnamerating age Sailors 1 102 9/ 2 102 9/ 2 101 9/ 2 103 9/ 1 102 9/ sid bid day Reserves 103 Santa Maria red 102 Pinta blue 101 Nina red bid bname color Boats SELECT S.name FROM Sailors S, reserves R WHERE S.sid = R.sid GROUP BY S.name, S.sid HAVING COUNT(DISTINCT R.bid) = ( Select COUNT (*) FROM Boats) 3 Frodo 1 102^ count Bilbo Bilbo Bilbo Frodo sname 2 101 2 102 2 103 1 102 sid bid Bilbo Frodo sname 2 101, 102, 103 1 102, sid bid Bilbo Frodo sname 2 3 1 1 sid count

More about Joins Explicit join semantics needed unless it is an INNER join (INNER is default) SELECT (column_list) FROM table_name [INNER | {LEFT |RIGHT | FULL } OUTER] JOIN table_name ON qualification_list WHERE …

Default semantics: Inner Join

Only rows that match search conditions are returned. SELECT s.sid, s.name, r.bid FROM Sailors s INNER JOIN Reserves r ON s.sid = r.sid Returns only those sailors who have reserved boats SQL-92 also allows: SELECT s.sid, s.name, r.bid FROM Sailors s NATURAL JOIN Reserves r “NATURAL” means equi-join for each pair of attributes with the same name SELECT s.sid, s.name, r.bid FROM Sailors s INNER JOIN Reserves r ON s.sid = r.sid

s.sid s.name r.bid

22 Dustin 101

95 Bob 103

Left Outer Join

Left Outer Join returns all matched rows, plus all unmatched rows from the table on the left of the join clause (use nulls in fields of non-matching tuples) SELECT s.sid, s.name, r.bid FROM Sailors s LEFT OUTER JOIN Reserves r ON s.sid = r.sid Returns all sailors & information on whether they have reserved boats SELECT s.sid, s.name, r.bid FROM Sailors s LEFT OUTER JOIN Reserves r ON s.sid = r.sid s.sid s.name r.bid 22 Dustin 101 95 Bob 103 31 Lubber

Right Outer Join

Right Outer Join returns all matched rows, plus all unmatched rows from the table on the right of the join clause SELECT r.sid, b.bid, b.name FROM Reserves r RIGHT OUTER JOIN Boats b ON r.bid = b.bid Returns all boats & information on which ones are reserved.

Null Values – 3 Valued Logic

NULL F T AND T F Null (null > 0) (null + 1) (null = 0) null AND true is null is null is null is null NULL F T OR T F Null T F Null T F F F Null F Null F T T T Null Null T Null