Professional Documents
Culture Documents
SQL Queries
SQL Queries
Principal form:
SELECT desired attributes
FROM tuple variables |
range over relations
WHERE condition about t.v.'s;
Running example relation schema:
Beersname, manf
Barsname, addr, license
Drinkersname, addr, phone
Likesdrinker, beer
Sellsbar, beer, price
Frequentsdrinker, bar
Example
name
Bud
Bud Lite
Michelob
manf
Anheuser-Busch
Anheuser-Busch
Anheuser-Busch
Renaming columns
Beersname, manf
SELECT name AS beer
FROM Beers
WHERE manf = 'Anheuser-Busch';
beer
Bud
Bud Lite
Michelob
bar
Joe's
Sue's
beer
Bud
Miller
priceInYen
300
360
drinker whoLikesBud
Sally likes Bud
Fred likes Bud
Example
Patterns
stands for any string.
stands for any one character.
Attribute LIKE pattern" is a condition that
is true if the string value of the attribute
matches the pattern.
Also NOT LIKE for negation.
Example
';
Nulls
10
Example
bar
Joe's bar
beer
Bud
price
NULL
SELECT bar
FROM Sells
WHERE price 2.00 OR price = 2.00;
-----------------------UNKNOWN
UNKNOWN
-----------------------UNKNOWN
11
3-Valued Logic
12
Multirelation Queries
List of relations in FROM clause.
Relation-dot-attribute disambiguates
attributes from several relations.
Example
13
Operational Semantics
14
drinker bar
Sally
drinker beer
Sally
Joe's
Likes
Frequents
15
Example
16
Subqueries
The IN Operator
Example
Also:
NOT IN
18
EXISTS
Example
Quanti ers
ANY
Example
Class Problem
20