sql success ch04

307
1 1 SQL Success Chapter 4 slides Stéphane Faroult

Upload: roughsea

Post on 03-Jul-2015

91 views

Category:

Education


0 download

DESCRIPTION

An SQL course

TRANSCRIPT

Page 1: Sql success ch04

1 1

SQL Success

Chapter 4 slides

Stéphane Faroult

Page 2: Sql success ch04

2

Page 3: Sql success ch04

3

Page 4: Sql success ch04

4

Page 5: Sql success ch04

5

Page 6: Sql success ch04

6

Page 7: Sql success ch04

7

Page 8: Sql success ch04

8

Page 9: Sql success ch04

9

Constraints

7

Page 10: Sql success ch04

10

movieid title country year_released 1 Casablanca us 1942 2 Goodfellas us 1990 3 Bronenosets Potyomkin ru 1925 4 Blade Runner us 1982 5 Annie Hall us 1977 6 Ying hung boon sik hk 1986 7 Sholay in 1975 8 On The Waterfront us 1954 9 Lawrence Of Arabia gb 1962 10 The Third Man gb 1949 11 Ladri di biciclette it 1948

movies

country_code country_name continent ru Russia EUROPE us United States AMERICA in India ASIA gb United Kingdom EUROPE fr France EUROPE hk Hong Kong ASIA it Italy EUROPE ca Canada AMERICA au Australia OCEANIA

countries

Page 11: Sql success ch04

11

select title, country_name, year_released from movies join countries on country_code = country where country_code <> 'us'

Page 12: Sql success ch04

12

select title, country_name, year_released from movies join countries on country_code = country where country_code <> 'us'

Page 13: Sql success ch04

13

select title, country_name, year_released from movies join countries on country_code = country where country_code <> 'us'

Page 14: Sql success ch04

14

select title, country_name, year_released from movies join countries on country_code = country where country_code <> 'us'

join on

Page 15: Sql success ch04

15

countries ru Russia EUROPE us United States AMERICA in India ASIA gb United Kingdom EUROPE fr France EUROPE hk Hong Kong ASIA it Italy EUROPE ca Canada AMERICA au Australia OCEANIA

movies 1 Casablanca us 1942 2 Goodfellas us 1990 3 Bronenosets Potyomkin ru 1925 4 Blade Runner us 1982 5 Annie Hall us 1977 6 Ying hung boon sik hk 1986 7 Sholay in 1975 8 On The Waterfront us 1954 9 Lawrence Of Arabia gb 1962 10 The Third Man gb 1949 11 Ladri di biciclette it 1948

Page 16: Sql success ch04

16

movies

ru Russia EUROPE us United States AMERICA us United States AMERICA

us United States AMERICA

us United States AMERICA

us United States AMERICA

gb United Kingdom EUROPE gb United Kingdom EUROPE

in India ASIA

it Italy EUROPE

hk Hong Kong ASIA

1 Casablanca us 1942 2 Goodfellas us 1990 3 Bronenosets Potyomkin ru 1925 4 Blade Runner us 1982 5 Annie Hall us 1977 6 Ying hung boon sik hk 1986 7 Sholay in 1975 8 On The Waterfront us 1954 9 Lawrence Of Arabia gb 1962 10 The Third Man gb 1949 11 Ladri di biciclette it 1948

joined to countries

Page 17: Sql success ch04

17

movies joined to countries

gb United Kingdom EUROPE

ru Russia EUROPE us United States AMERICA us United States AMERICA

us United States AMERICA

us United States AMERICA

us United States AMERICA

gb United Kingdom EUROPE

in India ASIA

it Italy EUROPE

hk Hong Kong ASIA

1 Casablanca us 1942 2 Goodfellas us 1990 3 Bronenosets Potyomkin ru 1925 4 Blade Runner us 1982 5 Annie Hall us 1977 6 Ying hung boon sik hk 1986 7 Sholay in 1975 8 On The Waterfront us 1954 9 Lawrence Of Arabia gb 1962 10 The Third Man gb 1949 11 Ladri di biciclette it 1948

select title, country_name, year_released from join on country_code = country where country_code <> 'us'

movies countries

Page 18: Sql success ch04

18

select title, country_name, year_released from join on country_code = country where country_code <> 'us'

countries

movies joined to countries

gb United Kingdom EUROPE

ru Russia EUROPE us United States AMERICA us United States AMERICA

us United States AMERICA

us United States AMERICA

us United States AMERICA

gb United Kingdom EUROPE

in India ASIA

it Italy EUROPE

hk Hong Kong ASIA

1 Casablanca us 1942 2 Goodfellas us 1990 3 Bronenosets Potyomkin ru 1925 4 Blade Runner us 1982 5 Annie Hall us 1977 6 Ying hung boon sik hk 1986 7 Sholay in 1975 8 On The Waterfront us 1954 9 Lawrence Of Arabia gb 1962 10 The Third Man gb 1949 11 Ladri di biciclette it 1948

movies

Page 19: Sql success ch04

19

on column1_from_table1 = column5_from_table2 and column2_from_table1 = column1_from_table2

Page 20: Sql success ch04

20

people peopleid first_name surname born died 5 Claude Rains 1889 1967 10 Lung Ti 1946 15 Carol Reed 1906 1976 20 Ramesh Sippy 1947 25 David Lean 1908 1991 30 Ray Liotta 1954 35 Rutger Hauer 1944

credits movieid peopleid credited_as 1 5 A 6 10 A 10 15 D 7 20 D 9 25 D 137 25 D 2 30 A 4 35 A

Page 21: Sql success ch04

21

First name and surname of all directors in the database?

select from people where credited_as = 'D'

first_name, surname

join credits

Page 22: Sql success ch04

22

First name and surname of all directors in the database?

select from people where credited_as = 'D'

first_name, surname

join credits

credits movieid peopleid credited_as 1 5 A 6 10 A 10 15 D 7 20 D 9 25 D 137 25 D 2 30 A 4 35 A 22

Page 23: Sql success ch04

23

First name and surname of all directors in the database?

select from people where credited_as = 'D'

first_name, surname

join credits

credits movieid peopleid credited_as 1 5 A 6 10 A 10 15 D 7 20 D 9 25 D 137 25 D 2 30 A 4 35 A 23

Page 24: Sql success ch04

24

First name and surname of all directors in the database?

select from people where credited_as = 'D'

first_name, surname

join credits

credits movieid peopleid credited_as 1 5 A 6 10 A 10 15 D 7 20 D 9 25 D 137 25 D 2 30 A 4 35 A 24

Page 25: Sql success ch04

25

First name and surname of all directors in the database?

select from people where credited_as = 'D'

first_name, surname distinct

join credits

Page 26: Sql success ch04

26

First name and surname of all directors in the database?

select from people where credited_as = 'D'

first_name, surname distinct

on peopleid = peopleid ? join credits

Page 27: Sql success ch04

27

natural join

First name and surname of all directors in the database?

select from people where credited_as = 'D'

first_name, surname distinct

credits

Page 28: Sql success ch04

28

natural join

First name and surname of all directors in the database?

select from people where credited_as = 'D'

first_name, surname distinct

credits

constraints

Page 29: Sql success ch04

29

natural join

First name and surname of all directors in the database?

select from people where credited_as = 'D'

first_name, surname distinct

credits

constraints names

Page 30: Sql success ch04

30

ctry name

countries

id name ctry

people

Page 31: Sql success ch04

31

ctry name

countries

id name ctry

people

Page 32: Sql success ch04

32

ctry name

countries

id name ctry

people

Page 33: Sql success ch04

33

ctry name

countries

id name ctry

people

Page 34: Sql success ch04

34

select distinct first_name, surname from people join credits where credited_as = 'D'

using (peopleid)

Page 35: Sql success ch04

35

select distinct first_name, surname from people join credits where credited_as = 'D' on credits.peopleid = people.peopleid

Page 36: Sql success ch04

36

select distinct first_name, surname from people join credits where credited_as = 'D'

on c.peopleid = p.peopleid

p c

Page 37: Sql success ch04

37

select distinct p.first_name, p.surname from people p join credits c on c.peopleid = p.peopleid where c.credited_as = 'D'

Page 38: Sql success ch04

38

Self-Join

Page 39: Sql success ch04

39

peopleid first_name surname ... fatherid motherid

876 MICHAEL REDGRAVE

1234 VANESSA REDGRAVE 876 932

932 RACHEL KEMPSON

Page 40: Sql success ch04

40

peopleid first_name surname ... fatherid motherid

876 MICHAEL REDGRAVE

1234 VANESSA REDGRAVE 876 932

932 RACHEL KEMPSON

Page 41: Sql success ch04

41

peopleid first_name surname ... fatherid motherid

876 MICHAEL REDGRAVE

1234 VANESSA REDGRAVE 876 932

932 RACHEL KEMPSON

Page 42: Sql success ch04

42

peopleid first_name surname ... fatherid motherid

876 MICHAEL REDGRAVE

1234 VANESSA REDGRAVE 876 932

932 RACHEL KEMPSON

select c.first_name || ' ' || c.surname as person, f.first_name || ' ' || f.surname as father from people as c -- child join people as f -- father on f.peopleid = c.fatherid

Page 43: Sql success ch04

43

[join operation] select ... from ( )x join ...

Page 44: Sql success ch04

44

from table1 inner join table2 inner join tablen

Page 45: Sql success ch04

45

British movie titles with director names?

Page 46: Sql success ch04

46

Page 47: Sql success ch04

47

credits

Page 48: Sql success ch04

48

A A D A A A D D A A D A

credits

Page 49: Sql success ch04

49

select m.title, p.surname

Page 50: Sql success ch04

50

P.C. Barua

1935

Page 51: Sql success ch04

51

P.C. Barua

1935

Page 52: Sql success ch04

52

P.C. Barua 1936 1935

Page 53: Sql success ch04

53

P.C. Barua 1936 1937

1935

Page 54: Sql success ch04

54

surname

Page 55: Sql success ch04

55

select m.title, p.surname

55

Page 56: Sql success ch04

56

select m.title, p.surname distinct

Page 57: Sql success ch04

57

select m.year_released, m.title, p.first_name, p.surname from movies m inner join credits c on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid where c.credited_as = 'D' and m.country = 'gb'

Page 58: Sql success ch04

58

select m.year_released, m.title, p.first_name, p.surname from movies m inner join credits c on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid where c.credited_as = 'D' and m.country = 'gb'

Page 59: Sql success ch04

59

select m.year_released, m.title, p.first_name, p.surname from movies m inner join credits c on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid where c.credited_as = 'D' and m.country = 'gb'

Page 60: Sql success ch04

60

select m.year_released, m.title, p.first_name, p.surname from movies m inner join credits c on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid where c.credited_as = 'D' and m.country = 'gb'

Page 61: Sql success ch04

61

Actors in any movie you like?

Page 62: Sql success ch04

62

select m.year_released, m.country, p.first_name, p.surname from people p join credits c on c.peopleid = p.peopleid join movies m on m.movieid = c.movieid

and c.credited_as = 'A'

Page 63: Sql success ch04

63

movies m

select m.year_released, m.title, p.first_name, p.surname from inner join on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid where c.credited_as = 'D' and m.country = 'gb'

credits c

Page 64: Sql success ch04

64

movies m

select m.year_released, m.title, p.first_name, p.surname from inner join on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid where c.credited_as = 'D' and m.country = 'gb'

credits c

Page 65: Sql success ch04

65

select m.year_released, m.title, p.first_name, p.surname from credits c where c.credited_as = 'D' and m.country = 'gb'

inner join movies m on c.movieid = m.movieid

inner join people p on p.peopleid = c.peopleid

Page 66: Sql success ch04

66

inner join people p on p.peopleid = c.peopleid

select m.year_released, m.title, p.first_name, p.surname from credits c where c.credited_as = 'D' and m.country = 'gb'

inner join movies m on c.movieid = m.movieid

Page 67: Sql success ch04

67

select m.year_released, m.title, p.first_name, p.surname from movies m, credits c, people p where c.movieid = m.movieid and p.peopleid = c.peopleid and c.credited_as = 'D' and m.country = 'gb'

Page 68: Sql success ch04

68

select m.year_released, m.title, p.first_name, p.surname from movies m, credits c, people p where c.movieid = m.movieid and p.peopleid = c.peopleid and c.credited_as = 'D' and m.country = 'gb'

Page 69: Sql success ch04

69

select m.year_released, m.title, p.first_name, p.surname from movies m, credits c, people p where c.movieid = m.movieid and p.peopleid = c.peopleid and c.credited_as = 'D' and m.country = 'gb'

Page 70: Sql success ch04

70

select m.year_released, m.title, p.first_name, p.surname from movies m join credits c on c.movieid = m.movieid join people p on p.peopleid = c.peopleid where c.credited_as = 'D' and m.country = 'it'

Page 71: Sql success ch04

71

select m.year_released, m.title from movies m where m.country = 'it'

Page 72: Sql success ch04

72

movies people

credits

Page 73: Sql success ch04

73

movies people

credits

Page 74: Sql success ch04

74

movies people

credits

Page 75: Sql success ch04

75

movies people

credits

Page 76: Sql success ch04

76

select m.year_released, m.title, p.first_name, p.surname from movies m, credits c, people p where c.movieid = m.movieid and p.peopleid = c.peopleid and c.credited_as = 'D' and m.country = 'it'

Page 77: Sql success ch04

77 Flickr: John Wigham

77

Page 78: Sql success ch04

78

outer join

Page 79: Sql success ch04

79

outer join inner

Page 80: Sql success ch04

80

Page 81: Sql success ch04

81

Page 82: Sql success ch04

82

Page 83: Sql success ch04

83

Page 84: Sql success ch04

84

left outer join

right outer join

full outer join

Page 85: Sql success ch04

85

left outer join

right outer join

full outer join

Page 86: Sql success ch04

86

Country Name Number of movies

Page 87: Sql success ch04

87

Country Name Number of movies

Page 88: Sql success ch04

88

select country as country_code, count(*) as number_of_movies from movies group by country

Page 89: Sql success ch04

89

select c.country_name, x.number_of_movies from countries c inner join (select country as country_code, count(*) as number_of_movies from movies group by country) x on x.country_code = c.country_code

select country as country_code, count(*) as number_of_movies from movies group by country

Page 90: Sql success ch04

90

select c.country_name, x.number_of_movies from countries c inner join (select country as country_code, count(*) as number_of_movies from movies group by country) x on x.country_code = c.country_code

Page 91: Sql success ch04

91

select c.country_name, x.number_of_movies from countries c left outer join (select country as country_code, count(*) as number_of_movies from movies group by country) x on x.country_code = c.country_code

Page 92: Sql success ch04

92

Display zero when we have no movies from a country?

Page 93: Sql success ch04

93

select c.country_name, case when x.number_of_movies is null then 0 else x.number_of_movies end number_of_movies from countries c left outer join (select country as country_code, count(*) as number_of_movies from movies group by country) x on x.country_code = c.country_code

Page 94: Sql success ch04

94

select c.country_name, coalesce(x.number_of_movies, 0) number_of_movies from countries c left outer join (select country as country_code, count(*) as number_of_movies from movies group by country) x on x.country_code = c.country_code

Page 95: Sql success ch04

95

from table1 left outer join table2 on

Page 96: Sql success ch04

96

from table1 left outer join table2 on

Page 97: Sql success ch04

97

from table1 left outer join table2 on

Page 98: Sql success ch04

98

from table1 left outer join table2 on

Page 99: Sql success ch04

99

from table1 left outer join table2 on

Page 100: Sql success ch04

100

from table1 left outer join table2 on

Page 101: Sql success ch04

101

from table1 left outer join table2 on

Page 102: Sql success ch04

102

from table1 left outer join table2 on

Page 103: Sql success ch04

103

British movie titles with director names

when available?

Page 104: Sql success ch04

104

select m.year_released, m.title, p.first_name, p.surname from movies m inner join credits c on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid where c.role = 'D' and m.country = 'gb'

Page 105: Sql success ch04

105

select m.year_released, m.title, p.first_name, p.surname from movies m inner join credits c on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid where c.role = 'D' and m.country = 'gb'

Page 106: Sql success ch04

106

select a.year_released, a.title, a.first_name, a.surname from (select m.year_released, m.title, m.country, p.first_name, p.surname, c.credited_as from movies m inner join credits c on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid) a

and a.country = 'gb'

Page 107: Sql success ch04

107

select m.year_released, m.title, p.first_name, p.surname from (select movieid, year_released, title from movies where country = 'gb') m inner join (select movieid, peopleid from credits where credited_as = 'D') c on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid

Page 108: Sql success ch04

108

select a.year_released, a.title, a.first_name, a.surname from (select m.year_released, m.title, m.country, p.first_name, p.surname, c.credited_as from movies m inner join credits c on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid) a where a.credited_as = 'D' and a.country = 'gb'

Page 109: Sql success ch04

109

select m.year_released, m.title, m.country, p.first_name, p.surname, c.credited_as from movies m inner join credits c on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid

Page 110: Sql success ch04

110

Which table will be fully shown?

Page 111: Sql success ch04

111

Which table will be fully shown?

movies

credits

people

Page 112: Sql success ch04

112

Which table will be fully shown?

movies

credits

people

Page 113: Sql success ch04

113

Which table will be fully shown?

movies

credits

people

Page 114: Sql success ch04

114

Which table will be fully shown?

movies

credits

people

Miss?

Page 115: Sql success ch04

115

select m.year_released, m.title, m.country, p.first_name, p.surname, c.credited_as from movies m left outer join credits c on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid

Page 116: Sql success ch04

116

from movies m left outer join credits c on c.movied = m.movieid

movies credits

Page 117: Sql success ch04

117

inner join people p on p.peopleid = c.peopleid

from movies m left outer join credits c on c.movied = m.movieid

movies credits

Page 118: Sql success ch04

118

inner join people p on p.peopleid = c.peopleid

from movies m left outer join credits c on c.movied = m.movieid

movies credits

Page 119: Sql success ch04

119

inner join people p on p.peopleid = c.peopleid

from movies m left outer join credits c on c.movied = m.movieid

movies credits people

Page 120: Sql success ch04

120

inner join people p on p.peopleid = c.peopleid

from movies m left outer join credits c on c.movied = m.movieid

movies credits people

Page 121: Sql success ch04

121

inner join people p on p.peopleid = c.peopleid

from movies m left outer join credits c on c.movied = m.movieid

movies credits people

Page 122: Sql success ch04

122

inner join people p on p.peopleid = c.peopleid

from movies m left outer join credits c on c.movied = m.movieid

movies credits people

Page 123: Sql success ch04

123

select m.year_released, m.title, m.country, p.first_name, p.surname, c.credited_as from movies m left outer join credits c on c.movieid = m.movieid people p on p.peopleid = c.peopleid

left outer join

Page 124: Sql success ch04

124

select a.year_released, a.title, a.first_name, a.surname from ( ) a where a.credited_as = 'D' and a.country = 'gb'

select m.year_released, m.title, m.country, p.first_name, p.surname, c.credited_as from movies m left outer join credits c on c.movieid = m.movieid people p on p.peopleid = c.peopleid

left outer join

Page 125: Sql success ch04

125

select a.year_released, a.title, a.first_name, a.surname from ( ) a where a.credited_as = 'D' and a.country = 'gb'

select m.year_released, m.title, m.country, p.first_name, p.surname, c.credited_as from movies m left outer join credits c on c.movieid = m.movieid people p on p.peopleid = c.peopleid

left outer join

Page 126: Sql success ch04

126

select a.year_released, a.title, a.first_name, a.surname from ( ) a where a.credited_as = 'D' and a.country = 'gb'

select m.year_released, m.title, m.country, p.first_name, p.surname, c.credited_as from movies m left outer join credits c on c.movieid = m.movieid left outer join people p on p.peopleid = c.peopleid

( or a.credited_as is null)

Page 127: Sql success ch04

127

select a.year_released, a.title, a.first_name, a.surname from ( ) a where a.credited_as = 'D' and a.country = 'gb'

select m.year_released, m.title, m.country, p.first_name, p.surname, c.credited_as from movies m left outer join credits c on c.movieid = m.movieid left outer join people p on p.peopleid = c.peopleid

( or a.credited_as is null)

Page 128: Sql success ch04

128 128

Page 129: Sql success ch04

129 129

Page 130: Sql success ch04

130

Hugh Grant

130

Page 131: Sql success ch04

131

Movies

Page 132: Sql success ch04

132

Movies

People

Page 133: Sql success ch04

133

Movies

People

Credits

Page 134: Sql success ch04

134

Movies

People

Credits

movieid peopleid

Page 135: Sql success ch04

135

Movies

People

Credits

movieid peopleid A

Page 136: Sql success ch04

136

select a.year_released, a.title, a.first_name, a.surname from ( ) a where a.credited_as = 'D' and a.country = 'gb'

select m.year_released, m.title, m.country, p.first_name, p.surname, c.credited_as from movies m left outer join credits c on c.movieid = m.movieid left outer join people p on p.peopleid = c.peopleid

( or a.credited_as is null)

Page 137: Sql success ch04

137

select a.year_released, a.title, a.first_name, a.surname from ( ) a where a.credited_as = 'D' and a.country = 'gb'

select m.year_released, m.title, m.country, p.first_name, p.surname, c.credited_as from movies m left outer join credits c on c.movieid = m.movieid left outer join people p on p.peopleid = c.peopleid

( or a.credited_as is null)

A

Page 138: Sql success ch04

138

select a.year_released, a.title, a.first_name, a.surname from ( ) a where a.credited_as = 'D' and a.country = 'gb'

select m.year_released, m.title, m.country, p.first_name, p.surname, c.credited_as from movies m left outer join credits c on c.movieid = m.movieid left outer join people p on p.peopleid = c.peopleid

( or a.credited_as is null)

A

Page 139: Sql success ch04

139

select m.year_released, m.title, p.first_name, p.surname from movies m left outer join credits c on c.movieid = m.movieid left outer join people p on p.peopleid = c.peopleid where (c.credited_as = 'D' or c.credited_as is null) and m.country = 'gb'

Page 140: Sql success ch04

140

select m.year_released, m.title, p.first_name, p.surname from movies m left outer join credits c on c.movieid = m.movieid left outer join people p on p.peopleid = c.peopleid where (c.credited_as = 'D' or c.credited_as is null) and m.country = 'gb'

Page 141: Sql success ch04

141

Get movie titles and director name if available

Page 142: Sql success ch04

142

Get movie titles and director name if available

Get movie titles and people involved, then display if

director known or no people found

Page 143: Sql success ch04

143

select m.year_released, m.title, p.first_name, p.surname from (select movieid, year_released, title from movies where country = 'gb') m inner join (select movieid, peopleid from credits where credited_as = 'D') c on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid

Page 144: Sql success ch04

144

select m.year_released, m.title, p.first_name, p.surname from (select movieid, year_released, title from movies where country = 'gb') m inner join (select movieid, peopleid from credits where credited_as = 'D') c on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid

Page 145: Sql success ch04

145

left outer

select m.year_released, m.title, p.first_name, p.surname from (select movieid, year_released, title from movies where country = 'gb') m join (select movieid, peopleid from credits where credited_as = 'D') c on c.movieid = m.movieid join people p on p.peopleid = c.peopleid

inner

Page 146: Sql success ch04

146

left outer

select m.year_released, m.title, p.first_name, p.surname from (select movieid, year_released, title from movies where country = 'gb') m join (select movieid, peopleid from credits where credited_as = 'D') c on c.movieid = m.movieid join people p on p.peopleid = c.peopleid

left outer

Page 147: Sql success ch04

147

Filter close to tables

Page 148: Sql success ch04

148

Joins

Page 149: Sql success ch04

149

Joins filtering

Page 150: Sql success ch04

150

Joins

qualifying

filtering

Page 151: Sql success ch04

151

select m.title, m.year_released from movies m inner join countries c on c.country_code = m.country where c.country_name = '...'

Page 152: Sql success ch04

152

select m.title, m.year_released from movies m inner join countries c on c.country_code = m.country where c.country_name = '...'

Filtering

Page 153: Sql success ch04

153

select m.title, c.country_name from movies m inner join countries c on c.country_code = m.country where m.year_released = ...

Qualifying

Page 154: Sql success ch04

154

select m.title, c.country_name from movies m inner join countries c on c.country_code = m.country where m.date_released = ...

UNLESS

Page 155: Sql success ch04

155

select m.title, c.country_name from movies m inner join countries c on c.country_code = m.country where m.date_released = ...

UNLESS

Page 156: Sql success ch04

156

select distinct m.title, c.country_name from movies m inner join countries c on c.country_code = m.country inner join credits cr on cr.movieid = m.movieid where m.year_released = ...

Page 157: Sql success ch04

157

Outer joins

Page 158: Sql success ch04

158

Outer joins

qualifying

Page 159: Sql success ch04

159

Outer joins filtering is null

Page 160: Sql success ch04

160

Join

Page 161: Sql success ch04

161

Join

Page 162: Sql success ch04

162

Join MORE rows?

Page 163: Sql success ch04

163

Join MORE rows?

filtering

Page 164: Sql success ch04

164

Join MORE rows?

filtering

qualifying

Page 165: Sql success ch04

165

Page 166: Sql success ch04

166

filtering ASAP

Page 167: Sql success ch04

167

filtering ASAP

Page 168: Sql success ch04

168

filtering ASAP

qualifying later

Page 169: Sql success ch04

169

filtering ASAP

qualifying later

Page 170: Sql success ch04

170

MOVIES

170

Page 171: Sql success ch04

171

US MOVIES

171

Page 172: Sql success ch04

172

US MOVIES, 1940s

172

Page 173: Sql success ch04

173

select * from movies where country_code = 'us' and year_released between 1940 and 1949

Page 174: Sql success ch04

174

select * from movies where country_code = 'us' and year_released between 1940 and 1949

select * from (select * from movies where country_code = 'us') us_movies where year_released between 1940 and 1949

Page 175: Sql success ch04

175

select * from movies where country_code = 'us' and year_released between 1940 and 1949

select * from movies where (country = 'us' or country = 'gb') and year_released between 1940 and 1949

Page 176: Sql success ch04

176

Set operators

Page 177: Sql success ch04

177

UNION

Page 178: Sql success ch04

178

US and GB movies, 1940s

Page 179: Sql success ch04

179

select movieid, title, year_released, country from movies where country = 'us' and year_released between 1940 and 1949

US and GB movies, 1940s

Page 180: Sql success ch04

180

select movieid, title, year_released, country from movies where country = 'us' and year_released between 1940 and 1949

select movieid, title, year_released, country from movies where country = 'gb' and year_released between 1940 and 1949

US and GB movies, 1940s

Page 181: Sql success ch04

181

select movieid, title, year_released, country from movies where country = 'us' and year_released between 1940 and 1949

select movieid, title, year_released, country from movies where country = 'gb' and year_released between 1940 and 1949

union

US and GB movies, 1940s

Page 182: Sql success ch04

182

Page 183: Sql success ch04

183

Page 184: Sql success ch04

184

Page 185: Sql success ch04

185

Page 186: Sql success ch04

186

movies

Page 187: Sql success ch04

187

movies

premium_movies

Page 188: Sql success ch04

188

select 'regular' as class, movieid, title, year_released from movies union select 'premium' as class, movieid, title, year_released from premium_movies

Page 189: Sql success ch04

189

select 'regular' as class, movieid, title, year_released from movies union select 'premium' as class, movieid, title, year_released from premium_movies

Page 190: Sql success ch04

190

select 'regular' as class, movieid, title, year_released from movies union select 'premium' as class, movieid, title, year_released from premium_movies

all

Page 191: Sql success ch04

191

current_year_data last_year_data

Page 192: Sql success ch04

192

current_year_data last_year_data

Page 193: Sql success ch04

193

current_year_data last_year_data

Page 194: Sql success ch04

194

current_year_data last_year_data

Page 195: Sql success ch04

195

current_year_data last_year_data

Page 196: Sql success ch04

196

current_year_data current_year_data last_year_data

Page 197: Sql success ch04

197

current_year_data current_year_data last_year_data

Page 198: Sql success ch04

198

Last year’s views plus year-to-date views

Page 199: Sql success ch04

199

select movieid, sum(view_count) as view_count from last_year_data group by movieid union select movieid, sum(view_count) as view_count from current_year_data group by movieid

Page 200: Sql success ch04

200

select movieid, sum(view_count) as view_count from last_year_data group by movieid union select movieid, sum(view_count) as view_count from current_year_data group by movieid

select x.movieid, sum(x.view_count) as view_count from (

) x group by x.movieid

Page 201: Sql success ch04

201

select movieid, sum(view_count) as view_count from last_year_data group by movieid union select movieid, sum(view_count) as view_count from current_year_data group by movieid

select x.movieid, sum(x.view_count) as view_count from (

) x group by x.movieid

Page 202: Sql success ch04

202

select movieid, sum(view_count) as view_count from last_year_data group by movieid union select movieid, sum(view_count) as view_count from current_year_data group by movieid

select x.movieid, sum(x.view_count) as view_count from (

) x group by x.movieid

Page 203: Sql success ch04

203

Goodfellas 2356

Goodfellas 2356

union

Page 204: Sql success ch04

204

Goodfellas 2356

Goodfellas 2356

union

Page 205: Sql success ch04

205

Goodfellas 2356

Goodfellas 2356

union all

Page 206: Sql success ch04

206

select x.movieid, sum(x.view_count) as view_count from (select 'last year' as period, movieid, sum(view_count) as view_count from last_year_data group by movieid union all select 'this year' as period, movieid, sum(view_count) as view_count from current_year_data group by movieid) x group by x.movieid

Page 207: Sql success ch04

207

Page 208: Sql success ch04

208

union

Page 209: Sql success ch04

209

intersect

Page 210: Sql success ch04

210

except / minus

Page 211: Sql success ch04

211

intersect

except

Page 212: Sql success ch04

212

intersect

except

inner join

Page 213: Sql success ch04

213

intersect

except

inner join

outer join

Page 214: Sql success ch04

214

country codes that are both in movies and countries

Page 215: Sql success ch04

215

select country_code from countries intersect select from movies

country

Page 216: Sql success ch04

216

select country_code from countries intersect select from movies

distinct country

Page 217: Sql success ch04

217

select c.country_code from countries c inner join ( ) m on m.country = c.country_code

select distinct country from movies

Page 218: Sql success ch04

218

select distinct country from movies

Page 219: Sql success ch04

219

select country_code from countries except select distinct country from movies

Page 220: Sql success ch04

220

select country_code from countries except select distinct country from movies

Page 221: Sql success ch04

221

select c.country_code from countries c left outer join (select distinct country from movies) m on m.country = c.country_code where m.country is null

Page 222: Sql success ch04

222

Equivalent columns

Page 223: Sql success ch04

223

Equivalent columns

Flickr: Tomáš Obšívač

Page 224: Sql success ch04

224

Names of the countries in table country but not in table movies?

Page 225: Sql success ch04

225

Names of the countries in table country but not in table movies? select c.country_ from countries c left outer join (select distinct country from movies) m on m.country = c.country_code where m.country is null

code

Page 226: Sql success ch04

226

name

Names of the countries in table country but not in table movies? select c.country_ from countries c left outer join (select distinct country from movies) m on m.country = c.country_code where m.country is null

Page 227: Sql success ch04

227

select * from movies where country_code = 'us'

Page 228: Sql success ch04

228

select * from movies where country_code = 'us'

select * from (select * from movies where country_code = 'us') us_movies where year_released between 1940 and 1949

Page 229: Sql success ch04

229

select * from movies where country_code = 'us'

select * from (select * from movies where country_code = 'us') us_movies where year_released between 1940 and 1949

Page 230: Sql success ch04

230

select * from movies where country_code = 'us'

select * from (select * from movies where country_code = 'us') us_movies where year_released between 1940 and 1949

Page 231: Sql success ch04

231

Page 232: Sql success ch04

232

Page 233: Sql success ch04

233

Page 234: Sql success ch04

234

Page 235: Sql success ch04

235

Page 236: Sql success ch04

236

Page 237: Sql success ch04

237

select m.title, m.year_released, c.country_name from movies m join countries c on c.country_code = m.country where m.country <> 'us'

Page 238: Sql success ch04

238

select m.title, m.year_released, (select c.country_name from countries c where c.country_code = m.country) as country_name from movies m where m.country <> 'us'

Page 239: Sql success ch04

239

select m.title, m.year_released, (select c.country_name from countries c where c.country_code = m.country) as country_name from movies m where m.country <> 'us'

Page 240: Sql success ch04

240

us

us

de in

gb

ru

us

in

Page 241: Sql success ch04

241

us

us

de in

gb

ru

us

in

select c.country_name from countries c where c.country_code = 'ru'

Page 242: Sql success ch04

242

us

us

de in

gb

ru

us

in select c.country_name from countries c where c.country_code = 'in'

Page 243: Sql success ch04

243

us

us

de in

gb

ru

us

in

Page 244: Sql success ch04

244

us

us

de in

gb

ru

us

in

Page 245: Sql success ch04

245

us

us

de in

gb

ru

us

in

select c.country_name from countries c where c.country_code = 'gb'

Page 246: Sql success ch04

246

us

us

de in

gb

ru

us

in

select c.country_name from countries c where c.country_code = 'de'

Page 247: Sql success ch04

247

us

us

de in

gb

ru

us

in

select c.country_name from countries c where c.country_code = 'in'

Page 248: Sql success ch04

248

select m.title, (select c.country_name from countries c where c.country_code = m.country) as country_name from movies m where m.country_code <> 'us'

Page 249: Sql success ch04

249

select m.title, c.country_name from movies m left outer join countries c on c.country_code = m.country where m.country <> 'us'

Page 250: Sql success ch04

250

from clause

select list

where clause

Page 251: Sql success ch04

251

from clause

select list

where clause

Page 252: Sql success ch04

252

from clause

select list

where clause

Page 253: Sql success ch04

253

in ( ..., ..., ... )

Page 254: Sql success ch04

254

in ( ..., ..., ... )

select country, title from movies where (country = 'us' or country = 'gb') and year_released between 1940 and 1949

Page 255: Sql success ch04

255

select country, title from movies where country in ('us', 'gb') and year_released between 1940 and 1949

in ( ..., ..., ... )

Page 256: Sql success ch04

256

in ( ..., ..., ... )

Page 257: Sql success ch04

257

in (select col from ... where ...)

Page 258: Sql success ch04

258

select country, year_released, title from movies where country in (select country_code from countries

Page 259: Sql success ch04

259

(col1, col2) in (select col3, col4 from t

Page 260: Sql success ch04

260

in

distinct

Page 261: Sql success ch04

261

select country, year_released, title from movies where country in (select country_code from countries

Page 262: Sql success ch04

262

select m.country, m.year_released, m.title from movies m inner join (select country_code from countries on c.country_code = m.country

Page 263: Sql success ch04

263

select m.country, m.year_released, m.title from movies m inner join (select country_code from countries on c.country_code = m.country

Page 264: Sql success ch04

264

no distinct Demonstrably unique

Page 265: Sql success ch04

265

select country, title from ... inner join (select distinct ... from ...) on ...

Page 266: Sql success ch04

266

select country, title from ... inner join (select distinct ... from ...) on ...

where col in (select distinct ... from ... )

Page 267: Sql success ch04

267

Explicit is better than

Implicit

Page 268: Sql success ch04

268

duplicate rows?

Page 269: Sql success ch04

269

duplicate rows?

nulls?

Page 270: Sql success ch04

270

col in ('a', 'b', 'c')

(col = 'a' or col = 'b' or col = 'c')

=

Page 271: Sql success ch04

271

col in ('a', 'b', null)

(col = 'a' or col = 'b' or col = null)

=

Page 272: Sql success ch04

272

col in ('a', 'b', null)

(col = 'a' or col = 'b' or col = null)

=

always

false

Page 273: Sql success ch04

273

Augustus de Morgan (1806-1871)

Page 274: Sql success ch04

274

col not in ('a', 'b', null)

(col <> 'a' and col <> 'b' and col <> null)

=

Page 275: Sql success ch04

275

col not in ('a', 'b', null)

(col <> 'a' and col <> 'b' and col <> null)

=

always

false

Page 276: Sql success ch04

276

col not in ('a', 'b', null)

(col <> 'a' and col <> 'b' and col <> null)

=

Page 277: Sql success ch04

277

select * from people where first_name not in (select first_name from people where born < 1950)

Page 278: Sql success ch04

278

select * from people where first_name not in (select first_name from people where born < 1950 and first_name is not null)

Page 279: Sql success ch04

279

select p.* from people p left outer join (select first_name, from people where born <= 1950) p2 on p2.first_name = p.first_name where p2.first_name is null

Page 280: Sql success ch04

280

select p.* from people p left outer join (select first_name, from people where born <= 1950) p2 on p2.first_name = p.first_name where p2.first_name is null

Page 281: Sql success ch04

281

select p.* from people p left outer join (select first_name, from people where born <= 1950) p2 on p2.first_name = p.first_name where p2.first_name is null and p.born >= 1950

Page 282: Sql success ch04

282

from clause

select list

where clause

Page 283: Sql success ch04

283

exists

Page 284: Sql success ch04

284

exists

not exists

Page 285: Sql success ch04

285

and exists (select ... ...)

Page 286: Sql success ch04

286

select distinct m.title from movies m where exists (select null from credits c inner join people p on p.peopleid = c.peopleid where c.credited_as = 'A' and p.born >= 1970 and c.movieid = m.movieid)

Page 287: Sql success ch04

287

select distinct m.title from movies m where exists (select null from credits c inner join people p on p.peopleid = c.peopleid where c.credited_as = 'A' and p.born >= 1970 and c.movieid = m.movieid)

Page 288: Sql success ch04

288 Flickr: Kevin N. Murphy

Page 289: Sql success ch04

289

select m.title from movies m where exists (select from credits c inner join people p on p.peopleid = c.peopleid where c.credited_as = 'A' and p.born >= 1970 and c.movieid = m.movieid)

c.movieid

Page 290: Sql success ch04

290

select m.title from movies m where exists (select from credits c inner join people p on p.peopleid = c.peopleid where c.credited_as = 'A' and p.born >= 1970 and c.movieid = m.movieid)

p.surname

Page 291: Sql success ch04

291

select m.title from movies m where exists (select from credits c inner join people p on p.peopleid = c.peopleid where c.credited_as = 'A' and p.born >= 1970 and c.movieid = m.movieid)

1

Page 292: Sql success ch04

292

'Insert anything here'

select m.title from movies m where exists (select from credits c inner join people p on p.peopleid = c.peopleid where c.credited_as = 'A' and p.born >= 1970 and c.movieid = m.movieid)

Page 293: Sql success ch04

293

select m.title from movies m where exists (select from credits c inner join people p on p.peopleid = c.peopleid where c.credited_as = 'A' and p.born >= 1970 and c.movieid = m.movieid)

null

Page 294: Sql success ch04

294

(select null from credits c inner join people p on p.peopleid = c.peopleid where c.created_as = 'A' and p.born >= 1970 and c.movieid = current movieid)

movies

Page 295: Sql success ch04

295

(select null from credits c inner join people p on p.peopleid = c.peopleid where c.created_as = 'A' and p.born >= 1970 and c.movieid = current movieid)

movies

Page 296: Sql success ch04

296

(select null from credits c inner join people p on p.peopleid = c.peopleid where c.created_as = 'A' and p.born >= 1970 and c.movieid = current movieid)

movies

Page 297: Sql success ch04

297

(select null from credits c inner join people p on p.peopleid = c.peopleid where c.created_as = 'A' and p.born >= 1970 and c.movieid = current movieid)

movies

Page 298: Sql success ch04

298

(select null from credits c inner join people p on p.peopleid = c.peopleid where c.created_as = 'A' and p.born >= 1970 and c.movieid = current movieid)

movies

Page 299: Sql success ch04

299

select distinct m.title from movies m where m.movieid in (select distinct c.movieid from credits c inner join people p on p.peopleid = c.peopleid where c.credited_as = 'A' and p.born >= 1970)

Page 300: Sql success ch04

300

correlated

uncorrelated

Page 301: Sql success ch04

301

correlated

uncorrelated from

Page 302: Sql success ch04

302

correlated

uncorrelated from

Page 303: Sql success ch04

303

It depends ...

Flickr: Alan Cleaver 303

Page 304: Sql success ch04

304

Joins on common values. Inner: match only Outer: complete with nulls

Page 305: Sql success ch04

305

Joins on common values. Inner: match only Outer: complete with nulls Order of tables and place of conditions matter with outer joins, not with inner joins.

Page 306: Sql success ch04

306

Joins on common values. Inner: match only Outer: complete with nulls Order of tables and place of conditions matter with outer joins, not with inner joins. Set operators also allow to combine datasets. Only UNION provides functionality that no operator provide.

Page 307: Sql success ch04

307

Joins on common values. Inner: match only Outer: complete with nulls Order of tables and place of conditions matter with outer joins, not with inner joins. Set operators also allow to combine datasets. Only UNION provides functionality that no operator provide. Subqueries can appear in the SELECT (correlated), in the FROM (uncorrelated), in the WHERE (either). Beware of nulls.