2_Introduction to SQL.pdf
-
Upload
jesusoptim -
Category
Documents
-
view
234 -
download
0
Transcript of 2_Introduction to SQL.pdf
-
8/12/2019 2_Introduction to SQL.pdf
1/61
1
Introduction to SQL
Select-From-Where Statements
Multirelation QueriesSubqueries
-
8/12/2019 2_Introduction to SQL.pdf
2/61
2
Why SQL?
SQL is a very-high-level language.
Say what to do rather than how to do it.
Avoid a lot of data-manipulation detailsneeded in procedural languages like C++ orJava.
Database management system figures
out best way to execute query. Called query optimization.
-
8/12/2019 2_Introduction to SQL.pdf
3/61
3
Select-From-Where Statements
SELECT desired attributesFROM one or more tables
WHERE condition about tuples ofthe tables
-
8/12/2019 2_Introduction to SQL.pdf
4/61
4
Our Running Example
All our SQL queries will be based on thefollowing database schema. Underline indicates key attributes.
Beers(name, manf)Bars(name, addr, license)
Drinkers(name, addr, phone)
Likes(drinker, beer)Sells(bar, beer, price)
Frequents(drinker, bar)
-
8/12/2019 2_Introduction to SQL.pdf
5/61
5
Example
Using Beers(name, manf), what beers aremade by Anheuser-Busch?
SELECT name
FROM Beers
WHERE manf = Anheuser-Busch;
-
8/12/2019 2_Introduction to SQL.pdf
6/61
6
Result of Query
nameBud
Bud LiteMichelob
. . .
The answer is a relation with a single attribute,name, and tuples with the name of each beerby Anheuser-Busch, such as Bud.
-
8/12/2019 2_Introduction to SQL.pdf
7/61
7
Meaning of Single-Relation Query
Begin with the relation in the FROMclause.
Apply the selection indicated by theWHERE clause.
Apply the extended projection indicated
by the SELECT clause.
-
8/12/2019 2_Introduction to SQL.pdf
8/61
8
Operational Semantics
Check ifAnheuser-Busch
name manf
Bud Anheuser-Busch Include t.namein the result, if so
Tuple-variable tloops over all
tuples
-
8/12/2019 2_Introduction to SQL.pdf
9/61
9
Operational Semantics --- General
Think of a tuple variable visiting eachtuple of the relation mentioned in FROM.
Check if the current tuple satisfies theWHERE clause.
If so, compute the attributes or
expressions of the SELECT clause usingthe components of this tuple.
-
8/12/2019 2_Introduction to SQL.pdf
10/61
10
* In SELECT clauses
When there is one relation in the FROMclause, * in the SELECT clause stands for
all attributes of this relation.
Example: Using Beers(name, manf):SELECT *
FROM BeersWHERE manf = Anheuser-Busch;
-
8/12/2019 2_Introduction to SQL.pdf
11/61
11
Result of Query:
name manf Bud Anheuser-Busch
Bud Lite Anheuser-BuschMichelob Anheuser-Busch
. . . . . .
Now, the result has each of the attributesof Beers.
-
8/12/2019 2_Introduction to SQL.pdf
12/61
12
Renaming Attributes
If you want the result to have differentattribute names, use AS torename an attribute.
Example: Using Beers(name, manf):
SELECT name AS beer, manf
FROM BeersWHERE manf = Anheuser-Busch
-
8/12/2019 2_Introduction to SQL.pdf
13/61
13
Result of Query:
beer manf Bud Anheuser-Busch
Bud Lite Anheuser-BuschMichelob Anheuser-Busch
. . . . . .
-
8/12/2019 2_Introduction to SQL.pdf
14/61
14
Expressions in SELECT Clauses
Any expression that makes sense canappear as an element of a SELECT clause.
Example: Using Sells(bar, beer, price):
SELECT bar, beer,
price*114 AS priceInYen
FROM Sells;
-
8/12/2019 2_Introduction to SQL.pdf
15/61
15
Result of Query
bar beer priceInYenJoes Bud 285
Sues Miller 342
-
8/12/2019 2_Introduction to SQL.pdf
16/61
16
Example: Constants as Expressions
Using Likes(drinker, beer):
SELECT drinker,likes Bud AS whoLikesBud
FROM Likes
WHERE beer = Bud;
-
8/12/2019 2_Introduction to SQL.pdf
17/61
17
Result of Query
drinker whoLikesBudSally likes Bud
Fred likes Bud
-
8/12/2019 2_Introduction to SQL.pdf
18/61
18
Example: Information Integration
We often build data warehouses fromthe data at many sources.
Suppose each bar has its own relation
Menu(beer, price) .
To contribute to Sells(bar, beer, price)
we need to query each bar and insertthe name of the bar.
-
8/12/2019 2_Introduction to SQL.pdf
19/61
19
Information Integration --- (2)
For instance, at Joes Bar we can issuethe query:
SELECT Joes Bar, beer, price
FROM Menu;
-
8/12/2019 2_Introduction to SQL.pdf
20/61
20
Complex Conditions in WHEREClause
Boolean operators AND, OR, NOT.Comparisons =, , , =.
And many other operators that produceboolean-valued results.
-
8/12/2019 2_Introduction to SQL.pdf
21/61
21
Example: Complex Condition
Using Sells(bar, beer, price), find the priceJoes Bar charges for Bud:
SELECT price
FROM Sells
WHERE bar = Joes Bar AND
beer = Bud;
-
8/12/2019 2_Introduction to SQL.pdf
22/61
22
Patterns
A condition can compare a string to apattern by:
LIKE or
NOT LIKE
Pattern is a quoted string with % =any string; _ = any character.
-
8/12/2019 2_Introduction to SQL.pdf
23/61
23
Example: LIKE
Using Drinkers(name, addr, phone) findthe drinkers with exchange 555:
SELECT name
FROM Drinkers
WHERE phone LIKE %555-_ _ _ _;
-
8/12/2019 2_Introduction to SQL.pdf
24/61
24
NULL Values
Tuples in SQL relations can have NULLas a value for one or more components.
Meaning depends on context. Two
common cases:Missing value: e.g., we know Joes Bar has
some address, but we dont know what it is.
Inapplicable: e.g., the value of attributespouse for an unmarried person.
-
8/12/2019 2_Introduction to SQL.pdf
25/61
25
Comparing NULLs to Values
The logic of conditions in SQL is really 3-valued logic: TRUE, FALSE, UNKNOWN.
Comparing any value (including NULL
itself) with NULL yields UNKNOWN.
A tuple is in a query answer iff the
WHERE clause is TRUE (not FALSE orUNKNOWN).
-
8/12/2019 2_Introduction to SQL.pdf
26/61
26
Three-Valued Logic
To understand how AND, OR, and NOTwork in 3-valued logic, think of TRUE =1, FALSE = 0, and UNKNOWN = .
AND = MIN; OR = MAX, NOT(x) = 1-x.Example:
TRUE AND (FALSE OR NOT(UNKNOWN))= MIN(1, MAX(0, (1 - ))) =
MIN(1, MAX(0, )) = MIN(1, ) = .
-
8/12/2019 2_Introduction to SQL.pdf
27/61
27
Surprising Example
From the following Sells relation:bar beer price
Joes Bar Bud NULL
SELECT bar
FROM Sells
WHERE price < 2.00 OR price >= 2.00;UNKNOWN UNKNOWN
UNKNOWN
-
8/12/2019 2_Introduction to SQL.pdf
28/61
28
Reason: 2-Valued Laws !=3-Valued Laws
Some common laws, like commutativityof AND, hold in 3-valued logic.
But not others, e.g., the law of the
excluded middle :pOR NOTp= TRUE.Whenp= UNKNOWN, the left side is
MAX( , (1 )) = != 1.
-
8/12/2019 2_Introduction to SQL.pdf
29/61
29
Multirelation Queries
Interesting queries often combine datafrom more than one relation.
We can address several relations in one
query by listing them all in the FROMclause.
Distinguish attributes of the same nameby . .
-
8/12/2019 2_Introduction to SQL.pdf
30/61
30
Example: Joining Two RelationsUsing relations Likes(drinker, beer) and
Frequents(drinker, bar), find the beers likedby at least one person who frequents JoesBar.
SELECT beer
FROM Likes, Frequents
WHERE bar = Joes Bar AND
Frequents.drinker =
Likes.drinker;
-
8/12/2019 2_Introduction to SQL.pdf
31/61
31
Formal Semantics
Almost the same as for single-relationqueries:
1. Start with the product of all the relations
in the FROM clause.2. Apply the selection condition from the
WHERE clause.
3. Project onto the list of attributes andexpressions in the SELECT clause.
-
8/12/2019 2_Introduction to SQL.pdf
32/61
32
Operational Semantics
Imagine one tuple-variable for eachrelation in the FROM clause.
These tuple-variables visit each
combination of tuples, one from eachrelation.
If the tuple-variables are pointing to
tuples that satisfy the WHERE clause,send these tuples to the SELECT clause.
-
8/12/2019 2_Introduction to SQL.pdf
33/61
33
Example
drinker bar drinker beer
tv1 tv2Sally Bud
Sally Joes
Likes
Frequentsto outputcheck these
are equal
checkfor Joe
-
8/12/2019 2_Introduction to SQL.pdf
34/61
34
Explicit Tuple-Variables
Sometimes, a query needs to use twocopies of the same relation.
Distinguish copies by following the
relation name by the name of a tuple-variable, in the FROM clause.
Its always an option to renamerelations this way, even when notessential.
-
8/12/2019 2_Introduction to SQL.pdf
35/61
35
Example: Self-Join
From Beers(name, manf), find all pairs
of beers by the same manufacturer. Do not produce pairs like (Bud, Bud).
Produce pairs in alphabetic order, e.g.
(Bud, Miller), not (Miller, Bud).SELECT b1.name, b2.name
FROM Beers b1, Beers b2
WHERE b1.manf = b2.manf AND
b1.name < b2.name;
-
8/12/2019 2_Introduction to SQL.pdf
36/61
36
SubqueriesA parenthesized SELECT-FROM-WHERE
statement (subquery) can be used as avalue in a number of places, includingFROM and WHERE clauses.
Example: in place of a relation in theFROM clause, we can use a subquery
and then query its result.Must use a tuple-variable to name tuples of
the result.
-
8/12/2019 2_Introduction to SQL.pdf
37/61
37
Example: Subquery in FROM
Find the beers liked by at least one personwho frequents Joes Bar.
SELECT beer
FROM Likes, (SELECT drinker
FROM Frequents
WHERE bar = Joes Bar)JDWHERE Likes.drinker = JD.drinker;
Drinkers whofrequent Joes Bar
-
8/12/2019 2_Introduction to SQL.pdf
38/61
38
Subqueries That Return One Tuple
If a subquery is guaranteed to produceone tuple, then the subquery can beused as a value.
Usually, the tuple has one component.A run-time error occurs if there is no tuple
or more than one tuple.
-
8/12/2019 2_Introduction to SQL.pdf
39/61
39
Example: Single-Tuple Subquery
Using Sells(bar, beer, price), find thebars that serve Miller for the same priceJoe charges for Bud.
Two queries would surely work:1. Find the price Joe charges for Bud.
2. Find the bars that serve Miller at that price.
-
8/12/2019 2_Introduction to SQL.pdf
40/61
40
Query + Subquery Solution
SELECT barFROM Sells
WHERE beer = Miller AND
price = (SELECT price
FROM Sells
WHERE bar = Joes BarAND beer = Bud);
The price atwhich Joesells Bud
-
8/12/2019 2_Introduction to SQL.pdf
41/61
41
The IN Operator
IN () is true if andonly if the tuple is a member of therelation produced by the subquery.
Opposite: NOT IN ().
IN-expressions can appear in WHEREclauses.
-
8/12/2019 2_Introduction to SQL.pdf
42/61
42
Example: IN
Using Beers(name, manf) and Likes(drinker,
beer), find the name and manufacturer ofeach beer that Fred likes.
SELECT *
FROM Beers
WHERE name IN (SELECT beer
FROM LikesWHERE drinker = Fred);
The set ofbeers Fredlikes
-
8/12/2019 2_Introduction to SQL.pdf
43/61
43
Remember These From Lecture #1?
SELECT aFROM R, S
WHERE R.b = S.b;
SELECT a
FROM R
WHERE b IN (SELECT b FROM S);
-
8/12/2019 2_Introduction to SQL.pdf
44/61
44
IN is a Predicate About Rs Tuples
SELECT aFROM R
WHERE b IN (SELECT b FROM S);
One loop, overthe tuples of R
a b1 2
3 4R
b c2 5
2 6S
(1,2) satisfiesthe condition;
1 is output once.
Two 2s
-
8/12/2019 2_Introduction to SQL.pdf
45/61
45
This Query Pairs Tuples from R, S
SELECT aFROM R, S
WHERE R.b = S.b;
Double loop, overthe tuples of R and S
a b1 2
3 4R
b c2 5
2 6S
(1,2) with (2,5)and (1,2) with
(2,6) both satisfythe condition;1 is output twice.
-
8/12/2019 2_Introduction to SQL.pdf
46/61
46
The Exists Operator
EXISTS() is true if and onlyif the subquery result is not empty.
Example: From Beers(name, manf) ,
find those beers that are the uniquebeer by their manufacturer.
-
8/12/2019 2_Introduction to SQL.pdf
47/61
47
Example: EXISTS
SELECT nameFROM Beers b1
WHERE NOT EXISTS (
SELECT *
FROM Beers
WHERE manf = b1.manf ANDname b1.name);
Set ofbeerswith thesamemanf asb1, butnot thesame
beer
Notice scope rule: manf refers
to closest nested FROM witha relation having that attribute.
Notice theSQL notequalsoperator
-
8/12/2019 2_Introduction to SQL.pdf
48/61
48
The Operator ANY
x= ANY() is a booleancondition that is true iffxequals at leastone tuple in the subquery result.
= could be any comparison operator.
Example: x>= ANY() means xis not the uniquely smallest tuple producedby the subquery.
Note tuples must have one component only.
-
8/12/2019 2_Introduction to SQL.pdf
49/61
-
8/12/2019 2_Introduction to SQL.pdf
50/61
50
Example: ALL
From Sells(bar, beer, price), find thebeer(s) sold for the highest price.
SELECT beer
FROM Sells
WHERE price >= ALL(
SELECT priceFROM Sells);
price from the outerSells must not beless than any price.
-
8/12/2019 2_Introduction to SQL.pdf
51/61
51
Union, Intersection, and Difference
Union, intersection, and difference ofrelations are expressed by the followingforms, each involving subqueries:
() UNION () () INTERSECT ()
() EXCEPT ()
-
8/12/2019 2_Introduction to SQL.pdf
52/61
52
Example: Intersection
Using Likes(drinker, beer), Sells(bar, beer,price), and Frequents(drinker, bar), findthe drinkers and beers such that:
1. The drinker likes the beer, and2. The drinker frequents at least one bar that
sells the beer.
Notice trick:
-
8/12/2019 2_Introduction to SQL.pdf
53/61
53
Solution
(SELECT * FROM Likes)INTERSECT
(SELECT drinker, beer
FROM Sells, Frequents
WHERE Frequents.bar = Sells.bar
);
The drinker frequentsa bar that sells the
beer.
subquery is
really a storedtable.
-
8/12/2019 2_Introduction to SQL.pdf
54/61
54
Bag Semantics
Although the SELECT-FROM-WHEREstatement uses bag semantics, thedefault for union, intersection, and
difference is set semantics. That is, duplicates are eliminated as the
operation is applied.
-
8/12/2019 2_Introduction to SQL.pdf
55/61
55
Motivation: Efficiency
When doing projection, it is easier toavoid eliminating duplicates.
Just work tuple-at-a-time.
For intersection or difference, it is mostefficient to sort the relations first.
At that point you may as well eliminate theduplicates anyway.
-
8/12/2019 2_Introduction to SQL.pdf
56/61
56
Controlling Duplicate Elimination
Force the result to be a set bySELECT DISTINCT . . .
Force the result to be a bag (i.e., dont
eliminate duplicates) by ALL, as in. . . UNION ALL . . .
-
8/12/2019 2_Introduction to SQL.pdf
57/61
57
Example: DISTINCT
From Sells(bar, beer, price), find all thedifferent prices charged for beers:
SELECT DISTINCT price
FROM Sells;
Notice that without DISTINCT, each
price would be listed as many times asthere were bar/beer pairs at that price.
-
8/12/2019 2_Introduction to SQL.pdf
58/61
58
Example: ALL
Using relations Frequents(drinker, bar) andLikes(drinker, beer):
(SELECT drinker FROM Frequents)
EXCEPT ALL(SELECT drinker FROM Likes);
Lists drinkers who frequent more bars than
they like beers, and does so as many timesas the difference of those counts.
-
8/12/2019 2_Introduction to SQL.pdf
59/61
59
Join Expressions
SQL provides several versions of (bag)joins.
These expressions can be stand-alone
queries or used in place of relations in aFROM clause.
-
8/12/2019 2_Introduction to SQL.pdf
60/61
60
Products and Natural Joins
Natural join:
R NATURAL JOIN S;
Product:
R CROSS JOIN S;Example:
Likes NATURAL JOIN Sells;
Relations can be parenthesized subqueries, aswell.
-
8/12/2019 2_Introduction to SQL.pdf
61/61
61
Theta JoinR JOIN S ON
Example: using Drinkers(name, addr) andFrequents(drinker, bar):
Drinkers JOIN Frequents ON
name = drinker;
gives us all (d, a, d, b) quadruples such
that drinker d lives at address a andfrequents bar b.