Lecture3 PDF
Lecture3 PDF
supplementary material:
“Database Management Systems” Sec. 4.1, 4.2, 5.1-5.6
class notes
2
Outline
• Combining relations
• Aggregation
3
Select-project-join SQL queries
select <attribute list>
required from <relation list>
where <condition>
optional and <condition>
and ...
• SPJ queries are the basic, and the most common, kind of a SQL query
• Because inputs and outputs are relations, SPJ queries are fully compositional -
this is called closure
4
Game of Thrones
Characters Episodes Appearances
name house season num (tle viewers name season num
5
Enter relational algebra
6
What is an algebra?
• A system consisting of operators and operands
• In relational algebra:
7
Relational algebra operations
1. The usual set operations: union ∪ , intersection ∩, set
difference ∖ , but applied to relations (sets of tuples)
2. Operations that remove parts of a relation
10
Projection
The projection operator, applied to relation π A1 , A2 ,..., An (R)
R, produces a new relation with a subsets
of R’s attributes.
Tyrion Lannister
Characters × Episodes
Daenerys Targaryen name house season num (tle
Tyrion Lannister 1 1 Winter is Coming
Episodes Tyrion Lannister 1 2 The Kingsroad
season num (tle Daenery Targaryen 1 1 Winter is Coming
sDaenery Targaryen 1 2 The Kingsroad
1 1 Winter is Coming
s
1 2 The Kingsroad
Tyrion Lannister
2 Jane 22 3 Mike 21
2 Jane 22 4 Dave 27
14
Natural join
The natural join of two relations R and S is a shorthand notation for a
join with the condition: the pair up tuples from R and S join if they R⨝S
agree on the values of the common attributes.
R S
sid name gpa sid did cid term grade
1111 Joe 3.2
1111 1 210 Fall 2012 A
2222 Ann 4.0
2222 1 220 Winter 2013
3333 Mike 3.5
R⨝S
R.sid R.name R.gpa S.sid S.did S.cid S.term S.grade
1111 Joe 3.2 1111 1 210 Fall 2012 A
2222 Ann 4.0 2222 1 220 Winter 2013
ρBarges (Boats)
16
Relational algebra operations (recap)
1. The usual set operations: union ∪ , intersection ∩, set
difference ∖ , but applied to relations (sets of tuples)
2. Operations that remove parts of a relation
18
Combining operations into queries
What are the names of students whose GPA is at least 3.5?
Ann
Mike
19
Combining operations into queries
What are the names of students who got an A in any course?
Students Enrollment
sid name gpa sid did cid term grade
1111 Joe 3.2 1111 1 210 Fall 2015 A
2222 Ann 4.0
2222 1 220 Winter 2016
3333 Mike 3.5
Joe
20
Combining operations into queries
What are the names of students who got an A in any course?
Students Enrollment
sid name gpa sid did cid term grade
1111 Joe 3.2 1111 1 210 Fall 2015 A
2222 Ann 4.0
2222 1 220 Winter 2016
3333 Mike 3.5
Joe
22
Examples
Sailors (sid, name, rating, age) Boats (bid, name, color) Reserves (sid, bid, day)
sid name ra(ng age bid name color sid bid day
1 DusQn 7 45 101 Interlake blue 1 101 10/10/12
2 Rusty 10 35 102 Interlake red 1 102 10/10/12
3 HoraQo 5 35 103 Clipper green 1 101 10/7/12
4 Zorba 8 18 104 Marine red 2 102 11/9/12
5 Julius 25
2 102 7/11/12
3 101 7/11/12
3 102 7/8/12
List ids of boats named Interlake 4 103 19/9/12
23
Examples (solution)
Sailors (sid, name, rating, age) Boats (bid, name, color) Reserves (sid, bid, day)
sid name ra(ng age bid name color sid bid day
1 DusQn 7 45 101 Interlake blue 1 101 10/10/12
2 Rusty 10 35 102 Interlake red 1 102 10/10/12
3 HoraQo 5 35 103 Clipper green 1 101 10/7/12
4 Zorba 8 18 104 Marine red 2 102 11/9/12
5 Julius 25
2 102 7/11/12
3 101 7/11/12
3 102 7/8/12
List ids of boats named Interlake 4 103 19/9/12
24
Examples
Sailors (sid, name, rating, age) Boats (bid, name, color) Reserves (sid, bid, day)
sid name ra(ng age bid name color sid bid day
1 DusQn 7 45 101 Interlake blue 1 101 10/10/12
2 Rusty 10 35 102 Interlake red 1 102 10/10/12
3 HoraQo 5 35 103 Clipper green 1 101 10/7/12
4 Zorba 8 18 104 Marine red 2 102 11/9/12
5 Julius 25
2 102 7/11/12
3 101 7/11/12
List ids of sailors who reserved boat 102
3 102 7/8/12
πsid ( σbid=102 Reserves) 4 103 19/9/12
25
Examples
Sailors (sid, name, rating, age) Boats (bid, name, color) Reserves (sid, bid, day)
sid name ra(ng age bid name color sid bid day
1 DusQn 7 45 101 Interlake blue 1 101 10/10/12
2 Rusty 10 35 102 Interlake red 1 102 10/10/12
3 HoraQo 5 35 103 Clipper green 1 101 10/7/12
4 Zorba 8 18 104 Marine red 2 102 11/9/12
5 Julius 25
2 102 7/11/12
3 101 7/11/12
List names of sailors who reserved the red
3 102 7/8/12
Interlake. 4 103 19/9/12
πSailors.name (
Sailors ⨝ (
( σname=Interlake and color=red Boats) ⨝ Reserves))
26
Examples
Sailors (sid, name, rating, age) Boats (bid, name, color) Reserves (sid, bid, day)
sid name ra(ng age bid name color sid bid day
1 DusQn 7 45 101 Interlake blue 1 101 10/10/12
2 Rusty 10 35 102 Interlake red 1 102 10/10/12
3 HoraQo 5 35 103 Clipper green 1 101 10/7/12
4 Zorba 8 18 104 Marine red 2 102 11/9/12
5 Julius 25
2 102 7/11/12
3 101 7/11/12
List names of boats that were reserved by 3 102 7/8/12
Horatio. 4 103 19/9/12
πBoats.name (
(σSailors.name=Horatio Sailors) ⨝
( Boats ⨝ Reserves))
any other way to do this?
27
Examples
Sailors (sid, name, rating, age) Boats (bid, name, color) Reserves (sid, bid, day)
sid name ra(ng age bid name color sid bid day
1 DusQn 7 45 101 Interlake blue 1 101 10/10/12
2 Rusty 10 35 102 Interlake red 1 102 10/10/12
3 HoraQo 5 35 103 Clipper green 1 101 10/7/12
4 Zorba 8 18 104 Marine red 2 102 11/9/12
5 Julius 25
2 102 7/11/12
3 101 7/11/12
List days on which some sailor with rating 3 102 7/8/12
higher than 7 was at sea 4 103 19/9/12
28
Examples (solution)
Sailors (sid, name, rating, age) Boats (bid, name, color) Reserves (sid, bid, day)
sid name ra(ng age bid name color sid bid day
1 DusQn 7 45 101 Interlake blue 1 101 10/10/12
2 Rusty 10 35 102 Interlake red 1 102 10/10/12
3 HoraQo 5 35 103 Clipper green 1 101 10/7/12
4 Zorba 8 18 104 Marine red 2 102 11/9/12
5 Julius 25
2 102 7/11/12
3 101 7/11/12
List days on which some sailor with rating 3 102 7/8/12
higher than 7 was at sea 4 103 19/9/12
29
Examples
Sailors (sid, name, rating, age) Boats (bid, name, color) Reserves (sid, bid, day)
sid name ra(ng age bid name color sid bid day
1 DusQn 7 45 101 Interlake blue 1 101 10/10/12
2 Rusty 10 35 102 Interlake red 1 102 10/10/12
3 HoraQo 5 35 103 Clipper green 1 101 10/7/12
4 Zorba 8 18 104 Marine red 2 102 11/9/12
5 Julius 25
2 102 7/11/12
3 101 7/11/12
List names and colors of boats that were 3 102 7/8/12
reserved by Zorba 4 103 19/9/12
30
Examples (solution)
Sailors (sid, name, rating, age) Boats (bid, name, color) Reserves (sid, bid, day)
sid name ra(ng age bid name color sid bid day
1 DusQn 7 45 101 Interlake blue 1 101 10/10/12
2 Rusty 10 35 102 Interlake red 1 102 10/10/12
3 HoraQo 5 35 103 Clipper green 1 101 10/7/12
4 Zorba 8 18 104 Marine red 2 102 11/9/12
5 Julius 25
2 102 7/11/12
3 101 7/11/12
List names and colors of boats that were 3 102 7/8/12
reserved by Zorba 4 103 19/9/12
πBoats.name, Boats.color (
(σname=Zorba Sailors) ⨝ Reserves) ⨝ Boats))
31
Renaming
Sometimes it is necessary to rename a relation, or
columns in the relation. For this, we use the renaming
operator.
ρBarges (Boats)
32
A self-join
A self-join is a join that joins together tuples from two
copies of the same table
2 Jane 22 F
3 Mike 21 M
4 Dave 27 M
πGirls.name, Boys.name (
ρGirls ( σgender=F People ) ⨝Girls.age<Boys.age
ρBoys ( σgender=M People ) )
33
Relational algebra - recap
34
Outline
• Part 2: SQL
• Combining relations
• Aggregation
35
SQL
• Made up of 2 parts:
36
SQL, relational algebra, do we need both?
Characters Episodes Appearances
name house season num (tle viewers name season num
10 tuples ><
Characters Appearances
37
SQL, relational algebra, do we need both?
Characters Episodes Appearances
name house season num (tle viewers name season num
1 tuple σ house=Trgaryen
Characters Appearances
38
SQL, relational algebra, do we need both?
Characters Episodes Appearances
name house season num (tle viewers name season num
40 tuples × Episodes
Characters Appearances
39
How do we pick a good rewriting?
• Good question! The answer: we don’t!
40
Architecture of a typical DBMS
Concurrency Recovery
Storage manager
control manager
transaction manager
index/record manager,
lock manager
buffer manager,
Storage disk space manager
41
Overview of query optimization
42
Operations on a single relation
R
id name age gender
1 Ann 18 F
Given a relation R, return a new
2 Jane 22 F
relation R’ that contains a sub-
3 Mike 21 M
set of the rows / columns from R
4 Dave 27 M
43
Selection in SQL
σ C (R) select *
from R
where C
44
Projection in SQL
π A1 , A2 ,..., An (R) select A1, A2, ..., An
from R
select name
π name (σ gpa≥3.5 (Students)) from Students
where gpa >= 3.5
name
Ann
Mike
47
Examples
Sailors (sid, name, rating, age) Boats (bid, name, color) Reserves (sid, bid, day)
sid name ra(ng age bid name color sid bid day
1 DusQn 7 45 101 Interlake blue 1 101 10/10/12
2 Rusty 10 35 102 Interlake red 1 102 10/10/12
3 HoraQo 5 35 103 Clipper green 1 101 10/7/12
4 Zorba 8 18 104 Marine red 2 102 11/9/12
5 Julius 25 2 102 7/11/12
3 101 7/11/12
(a) List names of boats. 3 102 7/8/12
4 103 19/9/12
(b) List ratings and ages sailors.
48
String expressions
Sailors (sid, name, rating, age) Boats (bid, name, color) Reserves (sid, bid, day)
Find name, age, 2*rating of all sailors whose name starts with A or
contains ‘ust’ as a substring starting from position 2
Q1 in Query.sql
select name, age, rating * 2 as twice_the_rating
from Sailors
where name like 'A%' or name like '_ust%';
50
Null values
• null stands for a missing / unknown / inapplicable value
1 DusQn 7 45 2 Rusty 10 35
2 Rusty 10 35 3 HoraQo 5 35
3 HoraQo 5 35 4 Zorba 8 18
4 Zorba 8 18 select *
5 Julius null 25 Q5 from Sailors
where rating is null;
sid name ra(ng age
5 Julius null 25
51
Null values (II)
• null stands for a missing / unknown / inapplicable value
3 HoraQo 5 35 3 0
4 Zorba 8 18 4 0
5 Julius null 25 5 null
52
Renaming attributes
select sid as sailor_id, rating / 10 as normalized_rating
Q7 from Sailors;
sailor_id normalized_ra(ng
1 0.7
2 1
3 0.5
4 0.8
5 null
53
Renaming relations
Q8 select S.sid, S.rating / 10 as normalized_rating
from Sailors S;
sid normalized_ra(ng
1 0.7
2 1
3 0.5
4 0.8
5 null
54
Sorting results
• Sets and bags are unordered collections
3. the optional desc keyword after each column specifies descending (higher
to lower) sort order; the default order is ascending (lower to higher)
55
Sorting results: example
Retrieve all reservations sorted by sid, Retrieve all reservations sorted by
with ties broken by bid sid (descending), with ties broken
by bid (ascending)
select * select *
Q9 from Reserves Q10 from Reserves
order by sid, bid order by sid desc, bid
56
Examples
Sailors (sid, name, rating, age) Boats (bid, name, color) Reserves (sid, bid, day)
bid name color sid bid day
sid name ra(ng age
101 Interlake blue 1 101 10/10/12
1 DusQn 7 45
102 Interlake red 1 102 10/10/12
2 Rusty 10 35
103 Clipper green 1 101 10/7/12
3 HoraQo 5 35
104 Marine red 2 102 11/9/12
4 Zorba 8 18
5 Julius 25 2 102 7/11/12
3 101 7/11/12
(a) Find the color of a boat named clipper. 3 102 7/8/12
4 103 19/9/12
(b) Find all sailors who are 35 or older, sort them by name.
(d) Find all boats that are either red or called Interlake.
57
Combining relations
R
id name age gender
1 Ann 18 F Informally: given relations R, S,
2 Jane 22 F return a new relation T that
3 Mike 21 M
contains some combination of
4 Dave 27 M
tuples from R, S
S
id major gpa
1 Math 3.7
2 CS 3.8
3 English 3.8
4 Philosophy 3.2
58
Cartesian product
List all pairs of Girls (G) and Boys (B)
G
id name age
select *
from G, B G×B
1 Ann 18 G.id G.name G.age B.id B.name B.age
2 Jane 22 1 Ann 18 3 Mike 21
B 1 Ann 18 4 Dave 27
4 Dave 27
List all pairs of Girls (G) and Boys (B) that involve Ann
select *
from G, B σ G .name=' Ann' (G × B)
where G.name = ‘Ann’
59
Cartesian product
List all pairs of Girls (G) and Boys (B) where the girl is younger than the boy
select *
G
σG.age<B.age (G ⨉B)
from G, B
id name age where G.age < B.age
1 Ann 18
2 Jane 22
G.id G.name G.age B.id B.name B.age
1 Ann 18 3 Mike 21
B 1 Ann 18 4 Dave 27
id name age 2 Jane 22 4 Dave 27
3 Mike 21
4 Dave 27
or is this a join?
yes, both are right, both are expressed by the same SQL query!
60
Join
Students Enrollment
sid name gpa sid did cid term grade
1111 Joe 3.2
1111 1 210 Fall 2012 A
2222 Ann 4.0
2222 1 220 Winter 2013 null
3333 Mike 3.5
List the name, GPA and enrollment information for all students
who have been enrolled in a course.
Students ⨝ Enrollment
select S.name, S.gpa, E.did, E.cid, E.term
S.name S.gpa E.did E.cid E.term from Students S, Enrollment E
Joe 3.2 1 210 Fall 2012 where S.sid = E.sid
Ann 4 1 220 Winter 2013
61
Join
Students Enrollment
sid name gpa
sid did cid term grade
1111 Joe 3.2
1111 1 210 Fall 2012 A
2222 Ann 4.0
2222 1 220 Winter 2013 null
3333 Mike 3.5
List the name, GPA and enrollment information for all students who
have been enrolled in a course and have a grade for that course.
Note that the same SQL query corresponds to both relational algebra
statements.
62
SQL is declarative
Students ⨝ σE.grade is not null (Enrollment)
σ E.grade is not null (Students ⨝ Enrollment)
select S.name, S.gpa, E.did, E.cid, E.term
from Students S, Enrollment E
where S.sid = E.sid
• the first statement first selects and then joins, while the second first
joins and then selects
• Both relational algebra statements return the same result, but the
processing costs differ
• SQL queries are declarative: we only say what to compute not how
• This makes it easier for us, and leads to a more efficient query execution
plan (because the database engine knows best)
63
More examples
Sailors (sid, name, rating, age) Boats (bid, name, color) Reserves (sid, bid, day)
bid name color sid bid day
sid name ra(ng age
101 Interlake blue 1 101 10/10/12
1 DusQn 7 45
102 Interlake red 1 102 10/10/12
2 Rusty 10 35
103 Clipper green 1 101 10/7/12
3 HoraQo 5 35
104 Marine red 2 102 11/9/12
4 Zorba 8 18
5 Julius null 25 2 102 7/11/12
3 101 7/11/12
3 102 7/8/12
4 103 19/9/12
DusQn
where S.sid = R.sid
and R.bid = 101 DusQn
HoraQo
64
More examples
Sailors (sid, name, rating, age) Boats (bid, name, color) Reserves (sid, bid, day)
bid name color sid bid day
sid name ra(ng age
101 Interlake blue 1 101 10/10/12
1 DusQn 7 45
102 Interlake red 1 102 10/10/12
2 Rusty 10 35
103 Clipper green 1 101 10/7/12
3 HoraQo 5 35
104 Marine red 2 102 11/9/12
4 Zorba 8 18
5 Julius null 25 2 102 7/11/12
3 101 7/11/12
3 102 7/8/12
4 103 19/9/12
65
More examples
Sailors (sid, name, rating, age) Boats (bid, name, color) Reserves (sid, bid, day)
bid name color sid bid day
sid name ra(ng age
101 Interlake blue 1 101 10/10/12
1 DusQn 7 45
102 Interlake red 1 102 10/10/12
2 Rusty 10 35
103 Clipper green 1 101 10/7/12
3 HoraQo 5 35
104 Marine red 2 102 11/9/12
4 Zorba 8 18
5 Julius null 25 2 102 7/11/12
3 101 7/11/12
3 102 7/8/12
4 103 19/9/12
66
How do we read a SQL query?
select S.name, B.name, R.day
from Sailors S, Reserves R, Boats B
where S.sid = R.sid
and R.bid = B.bid
and B.color = ‘green’
order by S.name, R.day
• First, look at the from clause - which tables are being used
• Next, look at the where where clause - what are the selection and
join conditions
• Then, look at the select clause - which columns are used in the
result
67
More examples
Sailors (sid, name, rating, age) Boats (bid, name, color) Reserves (sid, bid, day)
bid name color sid bid day
sid name ra(ng age
101 Interlake blue 1 101 10/10/12
1 DusQn 7 45
102 Interlake red 1 102 10/10/12
2 Rusty 10 35
103 Clipper green 1 101 10/7/12
3 HoraQo 5 35
104 Marine red 2 102 11/9/12
4 Zorba 8 18
5 Julius null 25 2 102 7/11/12
3 101 7/11/12
Find unique names and ratings of sailors who 3 102 7/8/12
Q14*
Find days of reservations made for a red boat
by a sailor who is under 40 years old.
Q15*
68
More examples
Sailors (sid, name, rating, age) Boats (bid, name, color) Reserves (sid, bid, day)
bid name color sid bid day
sid name ra(ng age
101 Interlake blue 1 101 10/10/12
1 DusQn 7 45
102 Interlake red 1 102 10/10/12
2 Rusty 10 35
103 Clipper green 1 101 10/7/12
3 HoraQo 5 35
104 Marine red 2 102 11/9/12
4 Zorba 8 18
5 Julius null 25 2 102 7/11/12
3 101 7/11/12
Find unique names and ratings of sailors who 3 102 7/8/12
69
More examples
Sailors (sid, name, rating, age) Boats (bid, name, color) Reserves (sid, bid, day)
bid name color sid bid day
sid name ra(ng age
101 Interlake blue 1 101 10/10/12
1 DusQn 7 45
102 Interlake red 1 102 10/10/12
2 Rusty 10 35
103 Clipper green 1 101 10/7/12
3 HoraQo 5 35
104 Marine red 2 102 11/9/12
4 Zorba 8 18
5 Julius null 25 2 102 7/11/12
3 101 7/11/12
3 102 7/8/12
70
Union, Intersect, Except
Find unique sids and names of sailors who have reserved a red
or a blue boat
select DISTINCT S.sid, S.name
Q16 from Sailors S, Boats B, Reserves R
where S.sid = R.sid and B.bid = R.bid
and (B.color = 'red' or B.color = 'blue')
71
Union, Intersect, Except
Find sids of sailors who have reserved a red or a blue boat
72
Union, Intersect, Except
Find sids of sailors who have reserved a red and a blue boat
select DISTINCT S.sid
Option 1 from Sailors S, Boats B1, Reserves R1,
Boats B2, Reserves R2
Q17 where S.sid = R1.sid and B1.bid = R1.bid
and S.sid = R2.sid and B2.bid = R2.bid
and B1.color = 'red'
and B2.color = 'blue';
73
Union, Intersect, Except
Find sids of sailors who have reserved a red but not a blue boat
EXCEPT
but not select
from
DISTINCT S.sid
Sailors S, Boats B, Reserves R
where S.sid = R.sid and B.bid = R.bid
and B.color = 'blue';
74
To use set operators...
... we must make sure that the sets involved in the
UNION / INTERSECT / EXCEPT are union-
compatible, i.e., tuples have the same number of
fields, and fields are of compatible types.
75
More examples
Find unique bids of boats that have been reserved by
sailors with rating > 7
Q21 select
from
distinct R.bid
Reserves R, Sailors S
where R.sid = S.sid
and S.rating > 7
Q22 select
from
R.bid
Reserves R, Sailors S
where R.sid = S.sid
and S.rating < 5
76
More examples
Find bids of boats that have been reserved by sailors
with rating > 7 and rating < 5
Q24*
77
More examples
Find bids of boats that have been reserved by sailors
with rating > 7 and rating < 5 select R.bid
from Reserves R, Sailors S
Q24 where R.sid = S.sid
and S.rating > 7
INTERSECT
select R.bid
from Reserves R, Sailors S
where R.sid = S.sid
and S.rating < 5
Q25 where
and
B.bid = R.bid
R.sid = S.sid
and S.rating > 7
EXCEPT
select R.bid
from Reserves R, Sailors S
where R.sid = S.sid
and S.rating < 5
78
Nested queries
79
Nested queries
Find names of sailors who have reserved boat 103
Option 1
select S.name
Q26 from Sailors S, Reserves R
where S.sid = R.sid
and R.bid = 103
80
Nested queries: examples
Find names of sailors who have reserved a red boat
Option 1
Q28 select
from
S.name
Boats B, Reserves R, Sailors S
where B.bid = R.bid
and R.sid = S.sid
and B.color = 'red'
Option 2
select S.name
Q29 from Sailors S
where S.sid in (select R.sid
from Reserves R
where R.bid in (select B.bid
from Boats B
where B.color = 'red'))
81
Nested queries: more examples
Write two queries that compute this: one nested query and
one using join.
Q30.a Q30.b
select distinct B.color select distinct B.color
from Boats B, Reserves R, Sailors S from Boats B, Reserves R
where B.bid = R.bid where B.bid = R.bid
and R.sid = S.sid and R.sid in (select S.sid
and S.rating > 7 from Sailors S
where S.rating > 7)
83
Nested queries: more examples
Write two queries that compute this: one nested query and
one using join.
84
Nested queries: examples
Find names of sailors who have never reserved a red boat
85
Aggregation
• In addition to retrieving data, we can use SQL to
summarize / help analyze it.
87
Aggregation: more examples
Find the name and age of the oldest sailor
Take 2 (works)
88
Aggregation: more examples
Count the number of sailors
select COUNT(*)
from Sailors
Q38
Q42*
90
Aggregation: more examples
Find the number of sailors who are older than the
oldest sailor with the rating of 10
91
Group-by and having clauses
Find the age of the youngest sailor for each rating level
select MIN(age) select MIN(age) select MIN(age)
from Sailors from Sailors from Sailors
where rating = 1 where rating = 2 where rating = 3
.....
92
Group-by and having clauses
Find the age of the youngest sailor for each rating level
Q44 select
from
rating, MIN(age)
Sailors
group by rating
93
Group-by and having clauses
Find the age of the youngest sailor for each rating level
Q44 select
from
rating, MIN(age)
Sailors
group by rating
Find the age of the youngest sailor for each rating level,
such that there are at least two sailors with that rating
Q45 select
from
rating, MIN(age)
Sailors
group by rating
having count(*) > 1
94
Group-by and having clauses
But we usually only use aggregate columns in the having clause anyway.
95
Aggregation: more examples
Find the number of reservations for each boat that was
reserved at least 3 times.
Q46*
Find the bid, name of each red boat that has been
reserved, along with an average rating and minimum age
of sailors who have reserved that boat.
Q47*
96
Aggregation: more examples
Find the number of reservations for each boat that was
reserved at least 3 times.
select bid, count(*) as num_reservations
Q46 from
group
Reserves
by bid
having count(*) >= 3
Find the bid, name of each red boat that has been
reserved, along with an average rating and minimum age
of sailors who have reserved that boat.
select B.bid, B.name, MIN(S.age), AVG(S.rating)
from Boats B, Reserves R, Sailors S
Q47 where B.bid = R.bid
and R.sid = S.sid
and B.color = 'red'
group by B.bid, B.name
97
Aggregation: more examples
Find the number of boat reservations by each sailor who is
at least 20 years old.
Q48*
98
Aggregation: more examples
Find the number of boat reservations by each sailor who is
at least 20 years old.
select R.sid, count (*)
Q48 from Reserves R, Sailors S
where R.sid = S.sid
and S.age >= 20
group by R.sid
99
Sorting results: more example
Retrieve all information about reservations, sorted by bid
select *
Q50 from Reserves
order by bid
100
Additional SQL features
• Outer joins
• Database modifications
101
Running example
Animals (aid, name, hid) Habitats (hid, name)
aid name hid hid name
1 elephant 101 101 African savanna
2 giraffe 101
102 North Pacific ocean
3 whale 102
103 Eurasian tundra
4 dolphin 102
5 okapi null
Q60 animal_name
elephant
habitat_name
African savanna
select A.name as animal_name,
H.name as habitat_name giraffe African savanna
from Animals A, Habitats H
where A.hid = H.hid
whale North Pacific ocean
dolphin North Pacific ocean
102
Left outer join
Animals (aid, name, hid) Habitats (hid, name)
aid name hid hid name
1 elephant 101 101 African savanna
2 giraffe 101 102 North Pacific ocean
3 whale 102
103 Eurasian tundra
4 dolphin 102
5 okapi null
List names of animals and, if their habitat is known, also list
their habitat. animal_name habitat_name
elephant African savanna
Q61 giraffe African savanna
select A.name as animal_name,
whale North Pacific ocean
H.name as habitat_name
from Animals A left outer join Habitats H dolphin North Pacific ocean
on (A.hid = H.hid)
okapi null
103
Right outer join
Animals (aid, name, hid) Habitats (hid, name)
aid name hid hid name
1 elephant 101 101 African savanna
2 giraffe 101
102 North Pacific ocean
3 whale 102
103 Eurasian tundra
4 dolphin 102
5 okapi null
List names of habitats and, if there are animals inhabiting
them, also list names of these animals.
animal_name habitat_name
elephant African savanna
Q62 giraffe African savanna
select A.name as animal_name,
H.name as habitat_name whale North Pacific ocean
from Animals A right outer join Habitats H
on (A.hid = H.hid) dolphin North Pacific ocean
106
Join vs. outer join
Animals (aid, name, hid) Habitats (hid, name)
A.aid A.name A.hid H.hid H.name
1 elephant 101 101 African savanna
3 whale 102 102 North Pacific ocean
5 okapi null 103 Eurasian tundra
select *
from Animals A full outer join Habitats H
on A.hid = H.hid
107
Examples
Q64 select
from
A.name as animal_name, H.name as habitat_name
Animals A left outer join Habitats H
on (A.hid = H.hid)
where A.name like '%o%'
108
More examples
Animals (aid, name, hid) Habitats (hid, name)
aid name hid hid name
1 elephant 101 101 African savanna
2 giraffe 101
102 North Pacific ocean
3 whale 102
103 Eurasian tundra
4 dolphin 102
5 okapi null
List names of habitats that are not known to be inhabited
by any animals.
Q67
109
More examples
Animals (aid, name, hid) Habitats (hid, name)
aid name hid hid name
1 elephant 101 101 African savanna
2 giraffe 101
102 North Pacific ocean
3 whale 102
103 Eurasian tundra
4 dolphin 102
5 okapi null
List names of habitats that are not know to be inhabited by
any animals.
Q67.a select
from
H.name as habitat_name
Habitats H left outer join Animals A
on (H.hid = A.hid)
where A.hid is null
Q67.b select
from
H.name as habitat_name
Animals A right outer join Habitats H
on (H.hid = A.hid)
where A.hid is null
110
More examples
Animals (aid, name, hid) Habitats (hid, name)
aid name hid hid name
1 elephant 101 101 African savanna
2 giraffe 101
102 North Pacific ocean
3 whale 102
103 Eurasian tundra
4 dolphin 102
5 okapi null
List names of habitats that are not know to be inhabited
by any animals.
Q67.c select H.name as habitat_name
from Habitats H
select H.name as habitat_name
EXCEPT
where H.hid in ( select H.name
select hid from Habitats from Animals A, Habitats H
EXCEPT where A.hid = H.hid
select hid from Animals
)
111
Database modifications
Insertion insert into R (A1, A2, ..., An) values (v1, v2, ..., vn)
Q73update
set
Animals
hid = 101
Q74 update
set
Animals
name = upper(name),
where hid is null hid = 101
112