Professional Documents
Culture Documents
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
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
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
15 / 99 16 / 99
Projection Examples - 4 Projection Examples - 4
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
21 / 99 22 / 99
23 / 99 24 / 99
Join Examples - 2 Join Examples - 2
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
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
31 / 99 32 / 99
Division Example Division Example
33 / 99 34 / 99
35 / 99 36 / 99
Division Example Intersection
37 / 99 38 / 99
I1A
PERSON#
1485
39 / 99 40 / 99
Intersection Example Union
41 / 99 42 / 99
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
45 / 99 46 / 99
47 / 99 48 / 99
Difference Example Difference Example
49 / 99 50 / 99
References Projection
I *: all columns
51 / 99 52 / 99
Query Examples Ordering Results
53 / 99 54 / 99
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
57 / 99 58 / 99
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
61 / 99 62 / 99
63 / 99 64 / 99
Grouping Processing Order
65 / 99 66 / 99
67 / 99 68 / 99
Query Examples Query Examples
69 / 99 70 / 99
71 / 99 72 / 99
Query Examples Query Examples
73 / 99 74 / 99
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
77 / 99 78 / 99
79 / 99 80 / 99
Join Expressions Outer Join
81 / 99 82 / 99
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
83 / 99 84 / 99
Outer Join Examples Query Examples
85 / 99 86 / 99
87 / 99 88 / 99
Subqueries Query Examples
89 / 99 90 / 99
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
93 / 99 94 / 99
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
97 / 99 98 / 99
References
Supplementary Reference
I A Gentle Introduction to SQL:
http://sqlzoo.net/
99 / 99