Professional Documents
Culture Documents
e.g:
Boats
bid
bname
colo
r
101
Nina
red
102
Pinta
blue
103
Santa
Maria
red
INTERSECT
SELECT R.sid
FROM Boats B,Reserves R
WHERE B.color = green
AND R.bid=B.bid
sid
1
2
sid
1
Reserves
105 Titanic
sid
bid
gree
n
day
sid
101
9/12
103
9/13
105
9/13
bname
color
101
Nina
red
102
Pinta
blue
103
105
Santa Maria
Titanic
red
green
si
d
bid
day
101
9/12
103
9/13
105
9/13
sid
sid
1
sid
1
Nested Queries
The WHERE clause can itself contain an SQL query!
(Actually, so can FROM and HAVING clauses can too)
SELECT S.sname
FROM Sailors S
WHERE S.sid IN (SELECT R.sid
FROM Reserves R
sname
WHERE R.bid=103)
Sailors
sid
Bilbo
S 1
Frodo
22
S 2
Bilbo
39
S 3
Sam
27
si
d
bid
day
101
9/12
103
9/13
105
9/13
sid
2
Nested Queries
This nested query was uncorrelated because the subquery
does not refer to anything in the enclosing query
SELECT S.sname
FROM Sailors S
WHERE S.sid IN (SELECT R.sid
FROM Reserves R
WHERE R.bid=103)
Nested Queries
SELECT S.sname
FROM Sailors S
WHERE EXISTS (SELECT *
FROM Reserves R
WHERE R.bid=103 AND R.sid=S.sid)
1
2
3
Sailors
sid
S 1
Reserves
22
S 2
Bilbo
39
S 3
Sam
27
si
d
bid
day
101
9/12
103
9/13
105
9/13
si
d
bid
day
103 9/13
Set-Comparison Operators
<tuple expression> IN <set expression>
True if <tuple> is a member of <set>
Also, NOT IN
EXISTS <set expression>
True if <set expression> evaluates to a set with at least one member
Also, NOT EXISTS
UNIQUE <set expression>
by
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
101
103
WHERE R.bid=B.bid
1
3
AND R.sid=S.sid))
2
Reserves
Sailors
sid
Boats
S 1
Frodo
22
S 2
Bilbo
39
S 3
Sam
27
bid
bname
color
101
Nina
red
103
Pinta
blue
R
R
R
R
R
si
d
bid
day
103
9/12
103
9/13
103
9/14
101
9/12
103
9/13
UNIQUE
Find the names of sailors whove reserved boat
#103 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)
1
2
3
Sailors
sid
S 1
Reserves
22
S 2
Bilbo
39
S 3
Sam
27
si
d
bid
day
103
9/12
103
9/13
103
9/13
si
d
bid
day
103 9/13
ANY
Find sailors whose rating is greater than that of
some sailor called Bilbo:
Correlated or
uncorrelated?
SELECT *
FROM Sailors S
WHERE S.rating > ANY(SELECT S2.rating
FROM Sailors S2
WHERE S2.sname=Bilbo)
S1
Uncorrelated!
S2
sid
Frodo
22
2
1
Bilbo
Frodo
2
7
39
22
Bilbo
39
Bilbo
39
Sam
27
Sam
27
Intermission
Aggregate Operators
S.age)
FROM Sailors S
WHERE S.rating=10
Aggregate Operators
Find name and age of the oldest sailor(s)
(S.age)
39
Bilbo
39
Sam
39
Sailors
sid
Frodo
22
Bilbo
39
Sam
27
Aggregate Operators
Find name and age of the oldest sailor(s)
Instead:
SELECT S.sname, S.age And then find the
FROM Sailors S
sailors(s) of that
age
WHERE S.age =
Find the maximum
(SELECT MAX (S2.age) age
FROM Sailors S2)
target-list contains:
list of column names from groupinglist
terms with aggregate operations (e.g., MIN (S.age)).
Group By Example
For each rating, find the age of the youngest
sailor with age 18
Sailors
SELECT S.rating, MIN
FROM Sailors S
WHERE S.age >= 18
GROUP BY S.rating
22
21
27
sid
Frodo
22
Bilbo
39
Sam
27
Pippin
21
Merry
17
Group 1 1
Frodo
22
Bilbo
39
Pippin
21
Sam
27
(S.age)
Group 2
Group 3 3
Boats
bid
bname
color
101
Nina
red
102
Pinta
blue
Santa
Maria
red
103
103
101
Reserves
103
9/13
101
9/14
101
9/14
sid
bid
day
102
9/12
103
9/13
101
9/14
101
9/14
age
33.0
45.0
35.0
55.5
35.0
rating
1
7
38
10
m-age
33.0
35.0
55.0
35.0
rusty
count
1
2
1
1
10
35.0
rating
7
35.0
Answer
Sailors
102
Bilbo
101
Bilbo
102
Frodo
102
Bilbo
103
sname
sid coun
t
Frodo
Bilbo
si
d
snam
e
ratin
g
age
Frodo
22
Bilbo
39
3
Sam
Boats
27
bid
bname
color
101
Nina
red
102
Pinta
blue
103
Santa
red
Maria
Reserves
count
sid
bid
day
102
9/12
102
9/12
sname
sid
bid
101
9/14
Frodo
102,102
Bilbo
101, 102,
102
9/10
103
9/13
Dustin
Lubber
Bob
7
8
3
s.sid
45.0
55.5
63.5
sid bid
day
22 101 10/10/96
95 103 11/12/96
s.name r.bid
22 Dustin
101
95 Bob
103
Dustin
Lubber
Bob
s.sid
7
8
3
45.0
55.5
63.5
sid bid
day
22 101 10/10/96
95 103 11/12/96
s.name r.bid
22 Dustin
101
95 Bob
103
31 Lubber
sid bid
day
22 101 10/10/96
95 103 11/12/96
r.sid
bid
101
102
103
104
b.bid
22
95
101
102
103
104
bname
Interlake
Interlake
Clipper
Marine
b.name
Interlake
Interlake
Clipper
Marine
color
blue
red
green
red
sid bid
day
22 101 10/10/96
95 103 11/12/96
r.sid
bid
101
102
103
104
b.bid
22
95
101
102
103
104
bname
Interlake
Interlake
Clipper
Marine
color
blue
red
green
red
b.name
Interlake
Interlake
Clipper
Marine
INSERT
INSERT [INTO] table_name
[(column_list)]
VALUES ( value_list)
INSERT [INTO] table_name [(column_list)]
<select statement>
Null Values
Values are sometimes
unknown (e.g., a rating has not been assigned or
inapplicable (e.g., no spouses name).
SQL provides a special value null for such situations.
The presence of null complicates many issues. E.g.:
Special operators needed to check if value is/is not null.
rating>8 - true or false when rating is null? What about
AND, OR and NOT connectives?
Need a 3-valued logic (true, false and unknown).
Meaning of constructs must be defined carefully. (e.g.,
WHERE clause eliminates rows that dont evaluate to true.)
New operators (in particular, outer joins) possible/needed.
is null
(null + 1)
is null
(null = 0)
is null
is null
AND
Null
OR
Null
Null
Null
Null
Null
F
NUL
L
F
Null
F
F
F
Null
F
NUL
L