SQL Ii SQL DML 1: Basic Single-Table Queries: R & G - Chapter 5
SQL Ii SQL DML 1: Basic Single-Table Queries: R & G - Chapter 5
R & G - Chapter 5
4 Form groups
Relation
cross-product 1 FROM GROUP BY & aggregate
1
Cross (Cartesian) Product Cross (Cartesian) Product
All pairs of tuples, concatenated All pairs of tuples, concatenated
Sailors Sailors
Reserves Reserves
sid sname rating age sid sname rating age
sid bid day sid bid day
1 Popeye 10 22 1 Popeye 10 22
1 102 9/12 1 102 9/12
2 OliveOyl 11 39 2 OliveOyl 11 39
2 102 9/13 2 102 9/13
3 Garfield 1 27 3 Garfield 1 27
1 101 10/01 1 101 10/01
4 Bob 5 19 4 Bob 5 19
sid sname rating age sid bid day How many rows in cross product?
1 Popeye 10 22 1 102 9/12 SELECT * FROM Sailors, Reserves;
1 Popeye 10 22 2 102 9/13
A B C D E
1 Popeye 10 22 1 101 10/01
3 4 3+4 = 7 3*4 = 12 3^4 = 81
2 OliveOyl 11 39 1 102 9/12
… … … … … … ... http://sqlfiddle.com/#!17/0f0ac/26
http://sqlfiddle.com/#!17/53815/3
2
SQL Calculator String Comparisons
SELECT S.sname
FROM Sailors S Old-school SQL
WHERE S.sname LIKE 'B_%'
SELECT log(1000) as three, http://sqlfiddle.com/#!17/53815/7
exp(ln(2)) as two,
cos(0) as one,
ln(2*3) = ln(2) + ln(3) as sanity;
SELECT S.sname
http://sqlfiddle.com/#!17/121b8/17 Standard
FROM Sailors S
WHERE S.sname ~ 'B.*' Regular Expressions
http://sqlfiddle.com/#!17/53815/8
A B C D E
SAME DIFFERENT -- -- --
http://sqlfiddle.com/#!17/0f0ac/12
3
Find sid’s of sailors who’ve Find sid’s of sailors who
reserved a red and a green boat have not reserved a boat
SELECT R.sid
FROM Boats B,Reserves R
WHERE R.bid=B.bid AND http://sqlfiddle.com/#!17/53815/35
(B.color='red' AND B.color='green')
SELECT S.sid
SELECT R.sid FROM Sailors S
FROM Boats B, Reserves R
WHERE R.bid=B.bid EXCEPT
AND B.color='red'
INTERSECT
SELECT R.sid SELECT S.sid
Same or FROM Boats B, Reserves R FROM Sailors S, Reserves R
different? WHERE R.bid=B.bid AND B.color='green'
WHERE S.sid=R.sid
A B C D E http://sqlfiddle.com/#!17/53815/16
SAME DIFFERENT -- -- --
• UNION
{A, B, C, D, E}
• INTERSECT
{A, B, C}
• EXCEPT
{D}
4
“ALL”: Multiset Semantics “ALL”: Multiset Semantics
R = {A, A, A, A, B, B, C, D} = {A(4), B(2), C(1), D(1)} R = {A, A, A, A, B, B, C, D} = {A(4), B(2), C(1), D(1)}
S = {A, A, B, B, B, C, E} = {A(2), B(3), C(1), E(1)} S = {A, A, B, B, B, C, E} = {A(2), B(3), C(1), E(1)}
Names of sailors who’ve reserved boat #102: Names of sailors who’ve not reserved boat #103:
SELECT S.sname subquery SELECT S.sname
FROM Sailors S FROM Sailors S
WHERE S.sid IN WHERE S.sid NOT IN
(SELECT R.sid (SELECT R.sid
FROM Reserves R FROM Reserves R
WHERE R.bid=102) WHERE R.bid=103)
http://sqlfiddle.com/#!17/53815/17 http://sqlfiddle.com/#!17/53815/18
5
A Tough One: “Division”
http://sqlfiddle.com/#!17/53815/21
ARGMAX? ARGMAX?
• The sailor with the highest rating • The sailor with the highest rating
6
ARGMAX? Join Variants
• The sailor with the highest rating SELECT (column_list)
FROM table_name
[INNER | {LEFT |RIGHT | FULL } {OUTER}] JOIN table_name
SELECT * SELECT * ON qualification_list
FROM Sailors S FROM Sailors S
WHERE S.rating >= ALL WHERE …
(SELECT S2.rating
ORDER BY rating DESC
FROM Sailors S2) LIMIT 1;
http://sqlfiddle.com/#!17/4215a/5
Same or
• INNER is default
different? • Inner join is akin to what we’ve learned
A B C D E
SAME and SAME but DIFFERENT, DIFFERENT, DIFFERENT,
so far, just with different syntax.
CORRECT INCORRECT 1st is correct 2nd is correct both incorrect
sid sname rating age sid bid day • Right Outer Join returns all matched rows, and
22 Dustin 7 45.0 preserves all unmatched rows from the table on the
22 101 10/10/96 right of the join clause
31 Lubber 8 55.5 95 103 11/12/96
95 Bob 3 63.5 SELECT r.sid, b.bid, b.bname
FROM Reserves2 r RIGHT OUTER JOIN Boats2 b
ON r.bid = b.bid;
http://sqlfiddle.com/#!17/54a88/2
• Returns all boats & information on which ones are
reserved.
• No match for b.bid? r.sid IS NULL!
7
SELECT r.sid, b.bid, b.bname
Full Outer Join
FROM Reserves2 r RIGHT OUTER JOIN Boats2 b
ON r.bid = b.bid;
bid name color • Full Outer Join returns all (matched or unmatched)
sid bid day 101 Interlake blue rows from the tables on both sides of the join clause
22 101 10/10/96 102 Interlake red
95 103 11/12/96 103 Clipper green SELECT r.sid, b.bid, b.bname
104 Marine red FROM Reserves2 r FULL OUTER JOIN Boats2 b
ON r.bid = b.bid
http://sqlfiddle.com/#!17/a7b2f/3
8
Views Instead of Relations in Queries Subqueries in FROM
CREATE VIEW Redcount
AS SELECT B.bid, COUNT(*) AS scount Like a “view on the fly”!
FROM Boats2 B, Reserves2 R
WHERE R.bid=B.bid AND B.color='red' SELECT bname, scount
GROUP BY B.bid; FROM Boats2 B,
(SELECT B.bid, COUNT (*)
SELECT * from redcount; FROM Boats2 B, Reserves2 R
WHERE R.bid = B.bid AND B.color = 'red'
GROUP BY B.bid) AS Reds(bid, scount)
WHERE Reds.bid=B.bid
AND scount < 10
SELECT bname, scount
FROM Redcount R, Boats2 B
WHERE R.bid=B.bid
AND scount < 10;
http://sqlfiddle.com/#!17/cfdb2/1 http://sqlfiddle.com/#!17/cfdb2/2
http://sqlfiddle.com/#!17/cfdb2/3 http://sqlfiddle.com/#!17/cfdb2/3
http://sqlfiddle.com/#!17/4215a/9
9
NULL in the WHERE clause NULL in comparators
• Consider a tuple where rating IS NULL.
http://sqlfiddle.com/#!17/36ca9/2
SELECT * FROM sailors SELECT rating = NULL FROM sailors;
WHERE rating > 8; SELECT rating < NULL FROM sailors;
SELECT rating >= NULL FROM sailors;
http://sqlfiddle.com/#!17/f35aa/4 http://sqlfiddle.com/#!17/f35aa/1
NOT T F N F F F F T F
SELECT count(*) FROM sailors;
F T N N
SELECT count(rating) FROM sailors;
10
NULLs: Summary Summary
• You’ve now seen SQL—you are armed.
• A declarative language
– Somebody has to translate to algorithms though…
• NULL op NULL is NULL – The RDBMS implementor ... i.e. you!
• WHERE NULL: do not send to output • The data structures and algorithms that make SQL
possible also power:
• Boolean connectives: 3-valued logic – NoSQL, data mining, scalable ML, network routing…
• Aggregates ignore NULL-valued inputs – A toolbox for scalable computing!
– That fun begins next week
– But
• We skirted questions of good database (schema) design
– a topic we’ll consider in greater depth later
11