0% found this document useful (0 votes)
51 views11 pages

SQL Ii SQL DML 1: Basic Single-Table Queries: R & G - Chapter 5

1) The document discusses basic single-table queries in SQL including SELECT, FROM, WHERE, GROUP BY, HAVING, and ORDER BY clauses. 2) It provides examples of queries that select columns, filter rows, group results, and order output. 3) Key concepts covered are the logical processing order of a SQL query and eliminating duplicates with DISTINCT.

Uploaded by

Brendan Ho
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views11 pages

SQL Ii SQL DML 1: Basic Single-Table Queries: R & G - Chapter 5

1) The document discusses basic single-table queries in SQL including SELECT, FROM, WHERE, GROUP BY, HAVING, and ORDER BY clauses. 2) It provides examples of queries that select columns, filter rows, group results, and order output. 3) Key concepts covered are the logical processing order of a SQL query and eliminating duplicates with DISTINCT.

Uploaded by

Brendan Ho
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

SQL DML 1:

Basic Single-Table Queries


SQL II • SELECT [DISTINCT] <column expression list>
FROM <single table>
[WHERE <predicate>]
[GROUP BY <column list>
[HAVING <predicate>] ]
[ORDER BY <column list>]
[LIMIT <integer>];

R & G - Chapter 5

Conceptual SQL Evaluation Putting it all together


SELECT [DISTINCT] target-list
FROM relation-list
WHERE qualification
GROUP BY grouping-list
HAVING group-qualification
• SELECT S.dept, AVG(S.gpa), COUNT(*)
Project away columns 3 6 FROM Students S
Eliminate WHERE S.gender = 'F'
(just keep those used in SELECT [DISTINCT]
duplicates GROUP BY S.dept
SELECT, GBY, HAVING) HAVING COUNT(*) >= 2
ORDER BY S.dept;
5
Apply selections 2 WHERE HAVING Eliminate
(eliminate rows) groups • http://sqlfiddle.com/#!17/67109/12

4 Form groups
Relation
cross-product 1 FROM GROUP BY & aggregate

Join Queries Query Semantics SELECT


FROM
[DISTINCT] target-list
relation-list
WHERE qualification
• SELECT [DISTINCT] <column expression list>
FROM <table1 [AS t1], ... , tableN [AS tn]>
[WHERE <predicate>]
[GROUP BY <column list> 1. FROM : compute cross product of tables.
[HAVING <predicate>] ]
[ORDER BY <column list>]; 2. WHERE : Check conditions, discard tuples that
fail.
3. SELECT : Specify desired fields in output.
4. DISTINCT (optional) : eliminate duplicate rows.

• Note: likely a terribly inefficient strategy!


– Query optimizer will find more efficient plans.

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

Find sailors who’ve reserved About Range Variables


a boat
• Optional!
SELECT S.sid
FROM Sailors AS S, Reserves AS R SELECT Sailors.sid, sname
WHERE S.sid=R.sid FROM Sailors, Reserves
WHERE Sailors.sid = Reserves.sid
http://sqlfiddle.com/#!17/53815/2

http://sqlfiddle.com/#!17/53815/3

About Range Variables Arithmetic Expressions


• Needed when ambiguity could arise.
– e.g., same table used multiple times in FROM
(“self-join”) SELECT S.age, S.age-5 AS age1, 2*S.age AS age2
SELECT x.sname, x.age, y.sname, y.age FROM Sailors AS S
FROM Sailors AS x, Sailors AS y WHERE S.sname = 'Popeye'
WHERE x.age > y.age http://sqlfiddle.com/#!17/53815/5
http://sqlfiddle.com/#!17/53815/4
SELECT S1.sname AS name1, S2.sname AS name2
sid sname rating age FROM Sailors AS S1, Sailors AS S2
1 Popeye 10 22 WHERE 2*S1.rating = S2.rating - 1
2 OliveOyl 11 39 http://sqlfiddle.com/#!17/53815/6
3 Garfield 1 27
4 Bob 5 19

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

Find sid’s of sailors who’ve


Combining Predicates reserved a red or a green boat
SELECT R.sid
FROM Boats B, Reserves R
WHERE R.bid=B.bid AND
(B.color='red' OR
B.color='green')
• Subtle connections between:
– Boolean logic in WHERE (i.e., AND, OR)
– Traditional Set operations (i.e.
INTERSECT, UNION)
• Let’s see some examples…

Find sid’s of sailors who’ve Find sid’s of sailors who’ve


reserved a red or a green boat reserved a red and a green boat
SELECT R.sid
FROM Boats B, Reserves R SELECT R.sid
FROM Boats B,Reserves R
WHERE R.bid=B.bid AND http://sqlfiddle.com/#!17/0f0ac/27
(B.color='red' OR WHERE R.bid=B.bid AND
B.color='green') (B.color='red' AND B.color='green')

SELECT R.sid SELECT R.sid


FROM Boats B, Reserves R ... or: FROM Boats B, Reserves R
WHERE R.bid=B.bid AND WHERE R.bid=B.bid
B.color='red' AND B.color='red'
UNION ALL INTERSECT
SELECT R.sid SELECT R.sid
Same or FROM Boats B, Reserves R FROM Boats B, Reserves R
WHERE R.bid=B.bid AND B.color='green'
different? WHERE R.bid=B.bid AND B.color='green'

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 -- -- --

Default: Set 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} S = {A, A, B, B, B, C, E} = {A(2), B(3), C(1), E(1)}
S = {A, A, B, B, B, C, E}

• UNION
{A, B, C, D, E}
• INTERSECT
{A, B, C}
• EXCEPT
{D}

“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)}

• UNION ALL: sum of cardinalities • UNION ALL: sum of cardinalities


{A(4+2), B(2+3), C(1+1), D(1+0), E(0+1)} {A(4+2), B(2+3), C(1+1), D(1+0), E(0+1)}
= {A, A, A, A, A, A, B, B, B, B, B, C, C, D, E} = {A, A, A, A, A, A, B, B, B, B, B, C, C, D, E}
• INTERSECT ALL: min of cardinalities
{A(min(4,2)), B(min(2,3)), C(min(1,1)),
D(min(1,0)), E(min(0,1))}
= {A, A, B, B, C}

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)}

• UNION ALL: sum of cardinalities • UNION ALL: sum of cardinalities


{A(4+2), B(2+3), C(1+1), D(1+0), E(0+1)} {A(4+2), B(2+3), C(1+1), D(1+0), E(0+1)}
= {A, A, A, A, A, A, B, B, B, B, B, C, C, D, E} = {A, A, A, A, A, A, B, B, B, B, B, C, C, D, E}
• INTERSECT ALL: min of cardinalities • INTERSECT ALL: min of cardinalities
{A(min(4,2)), B(min(2,3)), C(min(1,1)), {A(min(4,2)), B(min(2,3)), C(min(1,1)),
D(min(1,0)), E(min(0,1))} D(min(1,0)), E(min(0,1))}
= {A, A, B, B, C} = {A, A, B, B, C}
• EXCEPT ALL: difference of cardinalities • EXCEPT ALL: difference of cardinalities N.B.: Cardinal
{A(4-2), B(2-3), C(1-1), D(1-0), E(0-1)} numbers start
{A(4-2), B(2-3), C(1-1), D(1-0), E(0-1)} at 0
= {A, A, D} = {A, A, D}

Nested Queries: IN Nested Queries: NOT IN

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

Nested Queries with More on Set-Comparison


Correlation Operators
Names of sailors who’ve reserved boat #102: • we’ve seen: IN, EXISTS
• can also have: NOT IN, NOT EXISTS
SELECT S.sname • other forms: op ANY, op ALL
FROM Sailors S
WHERE EXISTS
(SELECT * Find sailors whose rating is greater than that of some
FROM Reserves R sailor called Popeye:
http://sqlfiddle.com/#!17/53815/20
WHERE R.bid=102 AND S.sid=R.sid)
SELECT *
http://sqlfiddle.com/#!17/53815/19 FROM Sailors S
WHERE S.rating > ANY
• The correlated subquery must be recomputed for (SELECT S2.rating
each Sailors tuple. FROM Sailors S2
– Think of correlated subquery as a function f(S.sid) WHERE S2.sname='Popeye')

5
A Tough One: “Division”
http://sqlfiddle.com/#!17/53815/21

Relational Division: “Find sailors who’ve reserved all boats.”


Said differently: “sailors with no counterexample missing boats”
SELECT S.sname
FROM Sailors S Sailors S such that ...

WHERE NOT EXISTS there is no boat B that...


(SELECT B.bid
FROM Boats B
WHERE NOT EXISTS (SELECT R.bid
FROM Reserves R
WHERE R.bid=B.bid
...is missing a Reserves tuple AND R.sid=S.sid ))
showing S reserved B $25 Amazon gift cert, free T-shirt, food

Support SQLFiddle ARGMAX?

• The sailor with the highest rating


SELECT MAX(S.rating)
FROM Sailors S;
http://sqlfiddle.com/#!17/53815/32

SELECT S.*, MAX(S.rating)


FROM Sailors S;
Same or
http://sqlfiddle.com/#!17/53815/24
different?
A B C D E
SAME and SAME but DIFFERENT, DIFFERENT, DIFFERENT,
CORRECT INCORRECT 1st is correct 2nd is correct both incorrect

ARGMAX? ARGMAX?
• The sailor with the highest rating • The sailor with the highest rating

SELECT * SELECT * SELECT *


FROM Sailors S FROM Sailors S FROM Sailors S
WHERE S.rating >= ALL WHERE S.rating =
WHERE S.rating >= ALL (SELECT S2.rating (SELECT MAX(S2.rating)
(SELECT S2.rating FROM Sailors S2) FROM Sailors S2)
FROM Sailors S2)
http://sqlfiddle.com/#!17/53815/25 http://sqlfiddle.com/#!17/53815/25 http://sqlfiddle.com/#!17/53815/21
Same or
different?
A B C D E
SAME and SAME but DIFFERENT, DIFFERENT, DIFFERENT,
CORRECT INCORRECT 1st is correct 2nd is correct both incorrect

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

Inner/Natural Joins Left Outer Join


SELECT s.sid, s.sname, r.bid all 3 are Returns all matched rows, and preserves all unmatched
FROM Sailors s, Reserves r
WHERE s.sid = r.sid equivalent! rows from the table on the left of the join clause
AND s.age > 20; (use nulls in fields of non-matching tuples)
SELECT s.sid, s.sname, r.bid
FROM Sailors s INNER JOIN Reserves r SELECT s.sid, s.sname, r.bid
ON s.sid = r.sid FROM Sailors2 s LEFT OUTER JOIN Reserves2 r
WHERE s.age > 20; http://sqlfiddle.com/#!17/4215a/10
ON s.sid = r.sid;
SELECT s.sid, s.sname, r.bid
FROM Sailors s NATURAL JOIN Reserves r
WHERE s.age > 20; Returns all sailors & bid for boat in any of their
reservations
• “NATURAL” means equi-join for each pair of attributes with the Note: no match for s.sid? r.bid IS NULL!
same name
– Try SELECT * with NATURAL JOIN: removes duplicate attributes in output!

SELECT s.sid, s.sname, r.bid


Right Outer Join
FROM Sailors2 s LEFT OUTER JOIN Reserves2 r
ON s.sid = r.sid;

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

• Returns all boats & all information on reservations


http://sqlfiddle.com/#!17/a7b2f/1 • No match for r.bid?
– b.bid IS NULL AND b.bname IS NULL!
• No match for b.bid?
– r.sid IS NULL!

SELECT r.sid, b.bid, b.bname SELECT r.sid, b.bid, b.bname


FROM Reserves2 r FULL OUTER JOIN Boats2 b FROM Reserves2 r FULL OUTER JOIN Boats2 b
ON r.bid = b.bid ON r.bid = b.bid

bid name color bid name color


sid bid day 101 Interlake blue sid bid day 101 Interlake blue
22 101 10/10/96 102 Interlake red 22 101 10/10/96 102 Interlake red
95 103 11/12/96 103 Clipper green 95 103 11/12/96 103 Clipper green
104 Marine red 104 Marine red

http://sqlfiddle.com/#!17/a7b2f/3

Note: in this case it is the same as the ROJ!


bid is a foreign key in reserves, so all reservations must
have a corresponding tuple in boats.

Views: Named Queries Better reserve a red boat!

CREATE VIEW view_name


AS select_statement
INSERT INTO Reserves2
Makes development simpler VALUES (31, 102, '2016-01-26');

Often used for security


Not “materialized”
CREATE VIEW Redcount
AS SELECT B.bid, COUNT(*) AS scount
FROM Boats2 B, Reserves2 R
WHERE R.bid=B.bid AND B.color='red'
GROUP BY B.bid;

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

WITH Can have many queries in


a.k.a. common table expression (CTE) WITH
Another “view on the fly” syntax:
Another “view on the fly” syntax:
WITH Reds(bid, scount) AS
WITH Reds(bid, scount) AS (SELECT B.bid, COUNT (*)
(SELECT B.bid, COUNT (*) FROM Boats2 B, Reserves2 R
FROM Boats2 B, Reserves2 R WHERE R.bid = B.bid AND B.color = 'red'
WHERE R.bid = B.bid AND B.color = 'red' GROUP BY B.bid),
GROUP BY B.bid) UnpopularReds AS
SELECT bname, scount
SELECT bname, scount FROM Boats2 B, Reds
FROM Boats2 B, Reds WHERE Reds.bid=B.bid
WHERE Reds.bid=B.bid AND scount < 10
AND scount < 10 SELECT * FROM UnpopularReds;

http://sqlfiddle.com/#!17/cfdb2/3 http://sqlfiddle.com/#!17/cfdb2/3

ARGMAX GROUP BY? Brief Detour: Null Values


• The sailor with the highest rating per
age • Field values are sometimes unknown
– SQL provides a special value NULL for such situations.
– Every data type can be NULL
WITH maxratings(age, maxrating) AS • The presence of null complicates many issues. E.g.:
(SELECT age, max(rating) – Selection predicates (WHERE)
FROM Sailors – Aggregation
GROUP BY age)
SELECT S.*
• But NULLs comes naturally from Outer joins
FROM Sailors S, maxratings m
WHERE S.age = m.age
AND S.rating = m.maxrating;

http://sqlfiddle.com/#!17/4215a/9

9
NULL in the WHERE clause NULL in comparators
• Consider a tuple where rating IS NULL.

INSERT INTO sailors VALUES


(11, 'Jack Sparrow', NULL, 35); Rule: (x op NULL) evaluates to … 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;

SELECT * FROM sailors WHERE rating = NULL;


Same or
different?
A B C D E
Jack Sparrow Jack Sparrow -- -- -- http://sqlfiddle.com/#!17/f35aa/6
in the output not in output

Explicit NULL Checks NULL at top of WHERE

Rule: Do not output a tuple WHERE NULL

SELECT * FROM sailors WHERE rating IS NULL;


SELECT * FROM sailors;

SELECT * FROM sailors WHERE rating IS NOT NULL;


SELECT * FROM sailors WHERE rating > 8;

SELECT * FROM sailors WHERE rating <= 8;

http://sqlfiddle.com/#!17/f35aa/4 http://sqlfiddle.com/#!17/f35aa/1

NULL in Boolean Logic NULL and Aggregation


AND T F N OR T F N
Three-valued logic:
T T F T T T

NOT T F N F F F F T F
SELECT count(*) FROM sailors;
F T N N
SELECT count(rating) FROM sailors;

SELECT * FROM sailors WHERE rating > 8 AND TRUE;


SELECT sum(rating) FROM sailors;

SELECT * FROM sailors WHERE rating > 8 OR TRUE;


SELECT avg(rating) FROM sailors;

SELECT * FROM sailors WHERE NOT (rating > 8);


http://sqlfiddle.com/#!17/f35aa/2 http://sqlfiddle.com/#!17/f35aa/7

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy