You are on page 1of 25

License

c 2002-2017 T. Uyar, Ş. Öğüdücü


Database Systems You are free to:


I Share – copy and redistribute the material in any medium or format
Relational Algebra I Adapt – remix, transform, and build upon the material

Under the following terms:


I Attribution – You must give appropriate credit, provide a link to the license,
H. Turgut Uyar Şule Öğüdücü
and indicate if changes were made.
I NonCommercial – You may not use the material for commercial purposes.
I ShareAlike – If you remix, transform, or build upon the material, you must
2002-2017 distribute your contributions under the same license as the original.
For more information:
https://creativecommons.org/licenses/by-nc-sa/4.0/
Read the full license:
https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode

1 / 99 2 / 99

Topics Closure

Relational Algebra
Introduction
Selection
Join I closure: input and output of all operations are relations
Set Operations
I output of one operation can be the input to another
I operations can be nested
SQL
Introduction
Join
Subqueries
Set Operations

3 / 99 4 / 99
Example Database Example Database

MOVIE PERSON
MOVIE# TITLE YEAR SCORE VOTES DIRECTOR#
PERSON# NAME
6 The Usual Suspects 1995 8.7 35027 639
9 Arnold Schwarzenegger
70 Being John Malkovich 1999 8.3 13809 1485
26 Johnny Depp 364 John Waters
107 Batman & Robin 1997 3.5 10577 105
59 Terry Gilliam 406 Patricia Arquette
110 Sleepy Hollow 1999 7.5 10514 148
97 David Cronenberg 503 John Malkovich
112 Three Kings 1999 7.7 10319 1070
103 Peter Hyams 615 Lars von Trier
151 Gattaca 1997 7.4 8388 2020
105 Joel Schumacher 639 Bryan Singer
213 Blade 1998 6.7 6885 2861
138 George Clooney 745 Udo Kier
228 Ed Wood 1994 7.8 6587 148
148 Tim Burton 793 Jude Law
251 End of Days 1999 5.5 6095 103
175 Jim Jarmusch 1070 David O. Russell
281 Dangerous Liaisons 1988 7.7 5651 292
187 Christina Ricci 1485 Spike Jonze
373 Fear and Loathing in Las Vegas 1998 6.5 4658 59
243 Uma Thurman 1641 Iggy Pop
432 Stigmata 1999 6.1 4141 2557
282 Cameron Diaz 2020 Andrew Niccol
433 eXistenZ 1999 6.9 4130 97
292 Stephen Frears 2259 Dario Argento
573 Dead Man 1995 7.4 3333 175
302 Benicio Del Toro 2557 Rupert Wainwright
1468 Europa 1991 7.6 1042 615
308 Gabriel Byrne 2861 Stephen Norrington
1512 Suspiria 1977 7.1 1004 2259
350 Jennifer Jason Leigh 3578 Traci Lords
1539 Cry-Baby 1990 5.9 972 364

5 / 99 6 / 99

Example Database Selection

CASTING
MOVIE# ACTOR# ORD
6 308 2 251 745 10
6 302 3 281 243 7
70 282 2 281 503 2
70 503 14 373 26 1 I selection: selecting tuples that satisfy a condition
107 9 1 373 187 6
107
107
138
243
2
4
373
373
282
302
8
2
relation WHERE condition
110 26 1 432 308 2
110 187 2 432 406 1
112 138 1 433 350 1 I output header = input header
112 1485 4 433 793 2
151 243 2 573 26 1
151 793 3 573 308 12
213 745 6 573 1641 6
213 3578 8 1468 745 3
228 26 1 1512 745 9
228 406 4 1539 26 1
251 9 1 1539 1641 5
251 308 2 1539 3578 7

7 / 99 8 / 99
Selection Examples - 1 Selection Examples - 2

I movies with more than 10000 votes (S1) I movies older than 1992, with scores higher than 7.5 (S2)
MOVIE WHERE (VOTES > 10000) MOVIE WHERE ((YEAR < YEAR(1992))
AND (SCORE > SCORE(7.5)))
S1
MOVIE# TITLE YEAR SCORE VOTES DIRECTOR#
S2
6 The Usual Suspects 1995 8.7 35027 639
70 Being John Malkovich 1999 8.3 13809 1485 MOVIE# TITLE YEAR SCORE VOTES DIRECTOR#
107 Batman & Robin 1997 3.5 10577 105 281 Dangerous Liaisons 1988 7.7 5651 292
110 Sleepy Hollow 1999 7.5 10514 148 1468 Europa 1991 7.6 1042 615
112 Three Kings 1999 7.7 10319 1070

9 / 99 10 / 99

Projection Projection Examples - 1

I titles of all movies (P1)


MOVIE { TITLE }
I projection: selecting a set of attributes
P1
relation { attribute_name [, ...] } TITLE
The Usual Suspects
Being John Malkovich Dangerous Liaisons
I output header = attribute list Batman & Robin Fear and Loathing in Las Vegas
Sleepy Hollow Stigmata
Three Kings eXistenZ
Gattaca Dead Man
Blade Europa
Ed Wood Suspiria
End of Days Cry-Baby

11 / 99 12 / 99
Projection Examples - 2 Projection Examples - 3

I titles and years of all movies (P2) I years of all movies (P3)
MOVIE { TITLE, YEAR } MOVIE { YEAR }

P2 P3
TITLE YEAR YEAR
Batman & Robin 1997 1995
Being John Malkovich 1999 Fear and Loathing in Las Vegas 1998 1999
Blade 1998 Gattaca 1997 1997
Cry-Baby 1990 Sleepy Hollow 1999 1998
Dangerous Liaisons 1988 Stigmata 1999 1994
Dead Man 1995 Suspiria 1977 1988
Ed Wood 1994 Three Kings 1999 1991
End of Days 1999 The Usual Suspects 1995 1977
Europa 1991 eXistenZ 1999 1990

13 / 99 14 / 99

Projection Examples - 4 Projection Examples - 4

I movies with votes more than 5000


and scores higher than 7.0 (P4A)
I titles of movies with votes more than 5000 MOVIE WHERE ((VOTES > 5000)
and scores higher than 7.0 (P4) AND (SCORE > SCORE(7.0)))

1. movies with votes more than 5000 P4A


and scores higher than 7.0 (P4A) MOVIE# TITLE YEAR SCORE VOTES DIRECTOR#

2. titles in P4A (P4) 6


70
The Usual Suspects
Being John Malkovich
1995
1999
8.7
8.3
35027
13809
639
1485
110 Sleepy Hollow 1999 7.5 10514 148
112 Three Kings 1999 7.7 10319 1070
151 Gattaca 1997 7.4 8388 2020
228 Ed Wood 1994 7.8 6587 148
281 Dangerous Liaisons 1988 7.7 5651 292

15 / 99 16 / 99
Projection Examples - 4 Projection Examples - 4

I titles in P4A (P4)


I titles of movies with votes more than 5000
P4A { TITLE }
and scores higher than 7.0 (P4)
P4 ( MOVIE
TITLE
WHERE ((VOTES > 5000)
Being John Malkovich
Dangerous Liaisons AND (SCORE > SCORE(7.0))) )
Ed Wood
Gattaca { TITLE }
Sleepy Hollow
Three Kings
The Usual Suspects

17 / 99 18 / 99

Join Join

I join: matching tuples of two relations I from the Cartesian product of the two relations,
over common values of one or more attributes selecting the tuples which have the same values
relation1 JOIN relation2 for the given attributes
I matching attributes are not repeated in the output
I natural join: over common values of all the attributes
I output header = relation1 header ∪ relation2 header
with the same name

19 / 99 20 / 99
Join Examples - 1 Join Examples - 1

I all movies and their directors (J1A)


MOVIE JOIN
I titles and director names of all movies (J1) (PERSON RENAME { PERSON# AS DIRECTOR# })

1. all movies and their directors (J1A) J1A


MOVIE# TITLE ... DIRECTOR# NAME
2. titles and director names in J1A (J1) 6 The Usual Suspects ... 639 Bryan Singer
70 Being John Malkovich ... 1485 Spike Jonze
107 Batman & Robin ... 105 Joel Schumacher
... ... ... ... ...
1468 Europa ... 615 Lars von Trier
1512 Suspiria ... 2259 Dario Argento
1539 Cry-Baby ... 364 John Waters

21 / 99 22 / 99

Join Examples - 1 Join Examples - 2

I titles and director names in J1A (J1)


J1A { TITLE, NAME } I titles, actor names and actor orders of all movies (J2)

J1 1. all movies and their casting data (J2A)


TITLE NAME
Batman & Robin Joel Schumacher 2. all data in J2A matched with persons (J2B)
Being John Malkovich Spike Jonze
Blade Stephen Norrington 3. titles, actor names and actor orders in J2B (J2)
... ...
Three Kings Spike Jonze
The Usual Suspects Bryan Singer
eXistenZ David Cronenberg

23 / 99 24 / 99
Join Examples - 2 Join Examples - 2

I all data in J2A matched with persons (J2B)


I all movies and their casting data (J2A)
J2A JOIN
MOVIE JOIN CASTING
(PERSON RENAME { PERSON# AS ACTOR# })

J2A
MOVIE# TITLE ... ACTOR# ORD J2B
MOVIE# TITLE ... ACTOR# ORD NAME
6 The Usual Suspects ... 302 3
6 The Usual Suspects ... 308 2 6 The Usual Suspects ... 302 3 Benicio Del Toro
70 Being John Malkovich ... 282 2 6 The Usual Suspects ... 308 2 Gabriel Byrne
70 Being John Malkovich ... 503 14 70 Being John Malkovich ... 282 2 Cameron Diaz
... ... ... ... ... 70 Being John Malkovich ... 503 14 John Malkovich
1539 Cry-Baby ... 26 1 ... ... ... ... ... ...
1539 Cry-Baby ... 1641 5 1539 Cry-Baby ... 26 1 Johnny Depp
1539 Cry-Baby ... 3578 7 1539 Cry-Baby ... 1641 5 Iggy Pop
1539 Cry-Baby ... 3578 7 Traci Lords

25 / 99 26 / 99

Join Examples - 2 Join Examples - 3

I titles, actor names and actor orders in J2B (J2)


J2B { TITLE, NAME, ORD }
I names of the actors in Johnny Depp’s movies (J3)
J2
TITLE NAME ORD
1. ids of Johnny Depp’s movies (J3A)
The Usual Suspects Benicio Del Toro 3 2. ids of the actors in the movies in J3A (J3B)
The Usual Suspects Gabriel Byrne 2
Being John Malkovich Cameron Diaz 2 3. names of the actors in J3B (J3)
Being John Malkovich John Malkovich 14
... ... ...
Cry-Baby Johnny Depp 1
Cry-Baby Iggy Pop 5
Cry-Baby Traci Lords 7

27 / 99 28 / 99
Join Examples - 3 Join Examples - 3

I ids of Johnny Depp’s movies (J3A) I ids of the actors in the movies in J3A (J3B)
(((PERSON RENAME { PERSON# AS ACTOR# }) (J3A JOIN CASTING) { ACTOR# }
JOIN CASTING)
WHERE (NAME = "Johnny Depp")) { MOVIE# } J3B
ACTOR#

J3A 26
187
MOVIE# 282
302
110
308
228
373 406
1641
573
3578
1539

29 / 99 30 / 99

Join Examples - 3 Division

I names of the actors in J3B (J3)


((J3B RENAME { ACTOR# AS PERSON# })
I division: from the tuples of the first relation,
JOIN PERSON) { NAME }
selecting the ones that match all the tuples
J3 of the second relation in a mediating relation
NAME
relation1 DIVIDEBY relation2
Johnny Depp
Christina Ricci PER (relation3)
Cameron Diaz
Benicio Del Toro
Gabriel Byrne
Patricia Arquette
Iggy Pop
Traci Lords

31 / 99 32 / 99
Division Example Division Example

I ids of Johnny Depp and Christina Ricci (V1A)


(PERSON
I titles of movies with Johnny Depp and Christina Ricci (V1)
WHERE ((NAME = "Johnny Depp")
1. ids of Johnny Depp and Christina Ricci (V1A) OR (NAME = "Christina Ricci")))
{ PERSON# }
2. ids of movies with all the actors in V1A (V1B)
3. titles of the movies in V1B (V1) V1A
PERSON#
26
187

33 / 99 34 / 99

Division Example Division Example

I ids of movies with all the actors in V1A (V1B)


I titles of movies in V1B (V1)
(MOVIE { MOVIE# })
DIVIDEBY (V1B JOIN MOVIE) { TITLE }
(V1A RENAME { PERSON# AS ACTOR# })
PER (CASTING { MOVIE#, ACTOR# }) V1
TITLE

V1B Fear and Loathing in Las Vegas


Sleepy Hollow
MOVIE#
110
373

35 / 99 36 / 99
Division Example Intersection

I product - division relationship:


V1B JOIN V1A ⊆ CASTING { MOVIE#, ACTOR# } I intersection: selecting the tuples found in both relations
relation1 INTERSECT relation2
MOVIE# ACTOR#
110 26
110 187 I output header = relation1 header = relation2 header
373 26
373 187

37 / 99 38 / 99

Intersection Example Intersection Example

I ids of all directors who also acted (I1A)


(MOVIE { DIRECTOR# }
I names of all directors who also acted (I1) RENAME { DIRECTOR# AS PERSON# })
INTERSECT
1. ids of all directors who also acted (I1A) (CASTING { ACTOR# }
2. names of all the persons in I1A (I1) RENAME { ACTOR# AS PERSON# })

I1A
PERSON#
1485

39 / 99 40 / 99
Intersection Example Union

I names of all persons in I1A (I1)


I union: selecting the tuples found in at least one of two relations
(I1A JOIN PERSON) { NAME }
relation1 UNION relation2
I1
NAME I output header = relation1 header = relation2 header
Spike Jonze

41 / 99 42 / 99

Union Example Union Example

I ids and director ids of all movies newer than 1997 (U1A)
I names of all directors and actors of all movies newer than 1997 (MOVIE WHERE (YEAR > YEAR(1997)))
(U1) { MOVIE#, DIRECTOR# }

1. ids and director ids of all movies newer than 1997 (U1A) U1A
MOVIE# DIRECTOR#
2. ids of all actors in the movies in U1A (U1B)
70 1485
3. ids of directors and actors in at least one of U1A and U1B (U1C) 110
112
148
1070
213 2861
4. names of all the persons in U1C (U1) 251 103
373 59
432 2557
433 97

43 / 99 44 / 99
Union Example Union Example

I ids of directors and actors in at least one of


I ids of all actors in the movies in U1A (U1B) U1A and U1B (U1C)

(U1A JOIN CASTING) { ACTOR# } (U1A { DIRECTOR# }


RENAME { DIRECTOR# AS PERSON# })
U1B UNION (U1B RENAME { ACTOR# AS PERSON# })
ACTOR#
9 350 U1C
26 406
138 503 PERSON#
187 745 9 187 745
282 793 26 282 793
302 1485 59 302 1070
308 3578 97 308 1485
103 350 2557
138 406 2861
148 503 3578

45 / 99 46 / 99

Union Example Difference

I names of all persons in U1C (U1)


(U1C JOIN PERSON) { NAME } I difference: selecting the tuples which are found in the first
but not in the second relation
U1
NAME relation1 MINUS relation2
Arnold Schwarzenegger George Clooney Rupert Wainwright
Benicio Del Toro Jennifer Jason Leigh Spike Jonze
Cameron Diaz John Malkovich Stephen Norrington I output header = relation1 header = relation2 header
Christina Ricci Johnny Depp Terry Gilliam
David Cronenberg Jude Law Tim Burton
David O. Russell Patricia Arquette Traci Lords
Gabriel Byrne Peter Hyams Udo Kier

47 / 99 48 / 99
Difference Example Difference Example

I names of all the actors who are not in J3B (D1)


(((CASTING { ACTOR# } MINUS J3B)
RENAME { ACTOR# AS PERSON# })
I names of all actors who haven’t played in Johnny Depp’s movies
JOIN PERSON) {NAME}
(D1)

1. ids of all actors who played in Johnny Depp’s movies (J3B) D1


NAME
2. names of all actors who are not in J3B (D1) Arnold Schwarzenegger
George Clooney
Jennifer Jason Leigh
John Malkovich
Jude Law
Spike Jonze
Udo Kier
Uma Thurman

49 / 99 50 / 99

References Projection

I selecting columns from a table:


Required Reading: Date SELECT [ ALL | DISTINCT ] column_name [, ...]
I
FROM table_name
Chapter 7: Relational Algebra
7.1. Introduction
I duplicate rows are allowed
7.2. Closure Revisited
I ALL: preserve duplicate rows (default)
7.4. The Original Algebra: Semantics I DISTINCT: take only one of duplicate rows

I *: all columns

51 / 99 52 / 99
Query Examples Ordering Results

I all data of all movies I ordering the rows in the result


SELECT * FROM MOVIE SELECT [ ALL | DISTINCT ] column_name [, ...]
FROM table_name
I titles and years of all movies
[ ORDER BY { column_name [ ASC | DESC ] }
SELECT TITLE, YR FROM MOVIE [, ...] ]

I years when movies were released


I ASC: in ascending order (default)
SELECT DISTINCT YR FROM MOVIE I DESC: in descending order

53 / 99 54 / 99

Query Examples Query Examples

I years when movies were released, in ascending order


SELECT DISTINCT YR FROM MOVIE I all data of movies ordered first by descending years
ORDER BY YR then by ascending titles

I
SELECT * FROM MOVIE
years when movies were released, in descending order
ORDER BY YR DESC, TITLE ASC
SELECT DISTINCT YR FROM MOVIE
ORDER BY YR DESC

55 / 99 56 / 99
Column Expressions Query Examples

I evaluating expressions over columns


SELECT [ ALL | DISTINCT ]
{ expression [ AS column_name ] } [, ...] I titles and total scores of all movies
FROM table_name SELECT TITLE, SCORE * VOTES
[ ORDER BY { column_name [ ASC | DESC ] } FROM MOVIE
[, ...] ]

I new columns can be named: AS


I name or index of column can be used for ordering

57 / 99 58 / 99

Query Examples Selection

I titles and total scores of all movies,


I selecting rows from a table
in descending order of total scores
SELECT TITLE, SCORE * VOTES AS POINTS SELECT [ ALL | DISTINCT ]
FROM MOVIE { expression [ AS column_name ] } [, ...]
ORDER BY POINTS DESC FROM table_name
[ WHERE condition ]
SELECT TITLE, SCORE * VOTES [ ORDER BY { column_name [ ASC | DESC ] }
FROM MOVIE [, ...] ]
ORDER BY 2 DESC

59 / 99 60 / 99
Query Examples Condition Expressions

I check if null:
I year of “Citizen Kane”
column_name IS { NULL | NOT NULL }
SELECT YR FROM MOVIE
WHERE (TITLE = ’Citizen Kane’) I set membership:
I titles of movies with scores less than 3 column_name IN (value_set)
and votes more than 10
I string comparison:
SELECT TITLE FROM MOVIE
WHERE ((SCORE < 3) AND (VOTES > 10)) column_name LIKE pattern

I %: matches any group of symbols, including empty string

61 / 99 62 / 99

Query Examples Query Examples

I titles of movies with unknown year


SELECT TITLE FROM MOVIE I titles and scores of “Police Academy” movies
WHERE (YR IS NULL)
SELECT TITLE, SCORE FROM MOVIE
I titles and years of movies in the years 1967, 1954 and 1988 WHERE (TITLE LIKE ’Police Academy%’)

SELECT TITLE, YR FROM MOVIE


WHERE (YR IN (1967, 1954, 1988))

63 / 99 64 / 99
Grouping Processing Order

I grouping selected rows


SELECT [ ALL | DISTINCT ]
I select the rows that satisfy the WHERE condition
{ expression [ AS column_name ] } [, ...]
I group the selected rows using the columns
FROM table_name
[ WHERE condition ] specified in the GROUP BY clause
I if no group, the entire result is one group
[ GROUP BY column_name [, ...] ]
[ HAVING condition ] I select the groups that satisfy the HAVING condition
[ ORDER BY { column_name [ ASC | DESC ] } I calculate the expressions in the column list
[, ...] ] I order the result on the columns specified in the ORDER BY clause

I result contains one row per group


I groups can be filtered: HAVING

65 / 99 66 / 99

Group Values Query Examples

I for every year, the number of movies


with score greater than 8.5 in that year
I one value for each group
SELECT YR, COUNT(ID) FROM MOVIE
I column used for grouping WHERE (SCORE > 8.5)
I result of an aggregate function on a column GROUP BY YR
I aggregate functions: COUNT SUM AVG MAX MIN I score of the favorite movie of every year,
I column name as parameter in ascending order of years
I null values are ignored SELECT YR, MAX(SCORE) FROM MOVIE
GROUP BY YR
ORDER BY YR

67 / 99 68 / 99
Query Examples Query Examples

I averages of movie scores


in the years where there are at least 25 movies
for which more than 40 people have voted,
I total number of votes in ascending order of years
SELECT SUM(VOTES) FROM MOVIE SELECT YR, AVG(SCORE)
FROM MOVIE
WHERE (VOTES > 40)
GROUP BY YR
HAVING (COUNT(ID) >= 25)
ORDER BY YR

69 / 99 70 / 99

Join Query Examples

I joining can be achieved using WHERE conditions


I name of the director of “Star Wars”
I list tables to join FROM clause
I SELECT PERSON.NAME
use dotted notation for columns with identical names
FROM MOVIE, PERSON
I processing order (conceptual): WHERE ((MOVIE.DIRECTORID = PERSON.ID)
I take Cartesian product of the tables AND (MOVIE.TITLE = ’Star Wars’))
I select rows that satisfy the WHERE condition
I ...

71 / 99 72 / 99
Query Examples Query Examples

I names of the actors in “Alien” I titles of Harrison Ford’s movies


SELECT PERSON.NAME SELECT MOVIE.TITLE
FROM MOVIE, PERSON, CASTING FROM MOVIE, PERSON, CASTING
WHERE ((MOVIE.TITLE = ’Alien’) WHERE ((PERSON.NAME = ’Harrison Ford’)
AND (CASTING.MOVIEID = MOVIE.ID) AND (CASTING.MOVIEID = MOVIE.ID)
AND (CASTING.ACTORID = PERSON.ID)) AND (CASTING.ACTORID = PERSON.ID))

73 / 99 74 / 99

Query Examples Query Examples

I titles and names of the lead actors of the movies in 1962 I number of movies John Travolta acted in every year
SELECT MOVIE.TITLE, PERSON.NAME SELECT MOVIE.YR, COUNT(MOVIEID)
FROM MOVIE, PERSON, CASTING FROM MOVIE, PERSON, CASTING
WHERE ((MOVIE.YR = 1962) WHERE ((PERSON.NAME = ’John Travolta’)
AND (CASTING.MOVIEID = MOVIE.ID) AND (CASTING.MOVIEID = MOVIE.ID)
AND (CASTING.ACTORID = PERSON.ID) AND (CASTING.ACTORID = PERSON.ID))
AND (CASTING.ORD = 1)) GROUP BY MOVIE.YR

75 / 99 76 / 99
Query Examples Table Expressions

I joins can be expressed as table expressions:


I titles and number of actors of the movies in 1978, SELECT ...
in descending order of actor counts FROM table_expression [ AS table_name ]
WHERE selection_condition
SELECT MOVIE.TITLE, COUNT(CASTING.ACTORID) ...
FROM MOVIE, CASTING
WHERE ((MOVIE.YR = 1978)
I product
AND (MOVIE.ID = CASTING.MOVIEID))
I using conditions
GROUP BY MOVIE.MOVIEID, MOVIE.TITLE
ORDER BY 2 DESC I over columns with the same name
I natural join
I outer join

77 / 99 78 / 99

Join Expressions Query Examples

I product I name of the director of “Star Wars”


SELECT ... SELECT PERSON.NAME
FROM table1_name CROSS JOIN table2_name FROM MOVIE, PERSON
... WHERE ((MOVIE.DIRECTORID = PERSON.ID)
AND (MOVIE.TITLE = ’Star Wars’))
I join using conditions
SELECT PERSON.NAME
SELECT ...
FROM MOVIE JOIN PERSON
FROM table1_name JOIN table2_name
ON (MOVIE.DIRECTORID = PERSON.ID)
ON condition
WHERE (MOVIE.TITLE = ’Star Wars’)
...

79 / 99 80 / 99
Join Expressions Outer Join

I over columns with the same name


SELECT ... I inner join: unmatched rows are excluded
FROM table1_name JOIN table2_name
I outer join: unmatched rows are included
USING (column_name [, ...])
... I columns from the other table are null
I repeated columns are taken once SELECT ...
FROM table1_name [ LEFT | RIGHT | FULL ]
I natural join [ OUTER ] JOIN table2_name
SELECT ... ...
FROM table1_name NATURAL JOIN table2_name
...

81 / 99 82 / 99

Outer Join Examples Outer Join Examples

I left outer join I right outer join

T1 T2 T1 T2
NUM NAME NUM VALUE NUM NAME NUM VALUE
1 a 1 xxx 1 a 1 xxx
2 b 3 yyy 2 b 3 yyy
3 c 5 zzz 3 c 5 zzz

SELECT * FROM T1 LEFT JOIN T2 SELECT * FROM T1 RIGHT JOIN T2


NUM NAME NUM VALUE NUM NAME NUM VALUE
1 a 1 xxx 1 a 1 xxx
2 b 3 c 3 yyy
3 c 3 yyy 5 zzz

83 / 99 84 / 99
Outer Join Examples Query Examples

I full outer join

I titles of movies with no known actors


T1 T2
NUM NAME NUM VALUE
1 a 1 xxx
SELECT MOVIE.TITLE
2 b 3 yyy FROM MOVIE LEFT JOIN CASTING
3 c 5 zzz
ON (CASTING.MOVIEID = MOVIE.ID)
SELECT * FROM T1 FULL JOIN T2 WHERE (CASTING.ACTORID IS NULL)
NUM NAME NUM VALUE
1 a 1 xxx
2 b
3 c 3 yyy
5 zzz

85 / 99 86 / 99

Self Join Query Examples

I titles of all movies with the same number of votes


I how to join columns in the same table? SELECT M1.TITLE, M2.TITLE
I give a new name to the table in the expression FROM MOVIE AS M1, MOVIE AS M2
WHERE (M1.VOTES = M2.VOTES)
AND (M1.ID < M2.ID)

87 / 99 88 / 99
Subqueries Query Examples

I using subquery results in condition expressions


I titles and scores of all movies with scores
SELECT ...
higher than that of “Star Wars”, in descending order of scores
WHERE expression operator
[ ALL | ANY ] (subquery) SELECT TITLE, SCORE FROM MOVIE
... WHERE ( SCORE >
( SELECT SCORE FROM MOVIE
I result of subquery must be compatible WHERE (TITLE = ’Star Wars’) )
I ALL: for all values from subquery ) ORDER BY SCORE DESC
I ANY: for at least one value from subquery

89 / 99 90 / 99

Query Examples Query Examples

I titles of movies with scores less than I titles of movies with less votes
the scores of all “Police Academy” movies than any movie made before 1930
SELECT TITLE FROM MOVIE SELECT TITLE FROM MOVIE
WHERE ( SCORE < ALL WHERE ((YR >= 1930) AND ( VOTES < ANY (
( SELECT SCORE FROM MOVIE ( SELECT VOTES FROM MOVIE
WHERE (TITLE LIKE ’Police Academy%’) ) WHERE (YR < 1930) )
) ))

91 / 99 92 / 99
Query Examples Query Examples

I names of actors who played with Johnny Depp


I names of actors with at least 10 lead roles
SELECT PERSON.NAME
FROM PERSON JOIN CASTING SELECT NAME FROM PERSON
ON (CASTING.ACTORID = PERSON.ID) WHERE (ID IN (
WHERE (CASTING.MOVIEID IN ( SELECT ACTORID FROM CASTING
SELECT CASTING.MOVIEID WHERE (ORD = 1)
FROM PERSON JOIN CASTING GROUP BY ACTORID
ON (CASTING.ACTORID = PERSON.ID) HAVING (COUNT(MOVIEID) >= 10)
WHERE (PERSON.NAME = ’Johnny Depp’)) ) )
) )

93 / 99 94 / 99

Set Operations Query Examples

I an operation on two subquery results I number of people who both directed and acted
I intersection: INTERSECT SELECT COUNT(*) FROM (
I union: UNION ( SELECT DISTINCT DIRECTORID FROM MOVIE )
INTERSECT
I difference: EXCEPT
( SELECT DISTINCT ACTORID FROM CASTING )
I duplicate rows are not allowed in the result ) AS DIRECTOR_ACTOR

95 / 99 96 / 99
Query Examples Query Examples

I number of people who worked in movies before 1930


SELECT COUNT(*) FROM ( I number of directors who have not acted
( SELECT DISTINCT DIRECTORID FROM MOVIE
SELECT COUNT(*) FROM (
WHERE (YR < 1930) )
( SELECT DISTINCT DIRECTORID FROM MOVIE )
UNION
EXCEPT
( SELECT DISTINCT ACTORID FROM CASTING
( SELECT DISTINCT ACTORID FROM CASTING )
WHERE (MOVIEID IN
) AS DIRECTOR_ONLY
( SELECT ID FROM MOVIE
WHERE (YR < 1930) )) )
) AS OLD_MOVIE_PERSON_IDS

97 / 99 98 / 99

References

Required Reading: Date


I Chapter 8: Relational Calculus
I 8.6. SQL Facilities
I Appendix B: SQL Expressions
I Chapter 19: Missing Information

Supplementary Reference
I A Gentle Introduction to SQL:
http://sqlzoo.net/

99 / 99

You might also like