Sql success ch04

Post on 03-Jul-2015

87 Views

Category:

Education

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

An SQL course

Transcript

1 1

SQL Success

Chapter 4 slides

Stéphane Faroult

2

3

4

5

6

7

8

9

Constraints

7

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

11

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

12

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

13

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

14

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

join on

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

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

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

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

19

on column1_from_table1 = column5_from_table2 and column2_from_table1 = column1_from_table2

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

21

First name and surname of all directors in the database?

select from people where credited_as = 'D'

first_name, surname

join credits

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

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

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

25

First name and surname of all directors in the database?

select from people where credited_as = 'D'

first_name, surname distinct

join credits

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

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

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

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

30

ctry name

countries

id name ctry

people

31

ctry name

countries

id name ctry

people

32

ctry name

countries

id name ctry

people

33

ctry name

countries

id name ctry

people

34

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

using (peopleid)

35

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

36

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

on c.peopleid = p.peopleid

p c

37

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

38

Self-Join

39

peopleid first_name surname ... fatherid motherid

876 MICHAEL REDGRAVE

1234 VANESSA REDGRAVE 876 932

932 RACHEL KEMPSON

40

peopleid first_name surname ... fatherid motherid

876 MICHAEL REDGRAVE

1234 VANESSA REDGRAVE 876 932

932 RACHEL KEMPSON

41

peopleid first_name surname ... fatherid motherid

876 MICHAEL REDGRAVE

1234 VANESSA REDGRAVE 876 932

932 RACHEL KEMPSON

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

43

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

44

from table1 inner join table2 inner join tablen

45

British movie titles with director names?

46

47

credits

48

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

credits

49

select m.title, p.surname

50

P.C. Barua

1935

51

P.C. Barua

1935

52

P.C. Barua 1936 1935

53

P.C. Barua 1936 1937

1935

54

surname

55

select m.title, p.surname

55

56

select m.title, p.surname distinct

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'

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'

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'

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'

61

Actors in any movie you like?

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'

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

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

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

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

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'

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'

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'

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'

71

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

72

movies people

credits

73

movies people

credits

74

movies people

credits

75

movies people

credits

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'

77 Flickr: John Wigham

77

78

outer join

79

outer join inner

80

81

82

83

84

left outer join

right outer join

full outer join

85

left outer join

right outer join

full outer join

86

Country Name Number of movies

87

Country Name Number of movies

88

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

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

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

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

92

Display zero when we have no movies from a country?

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

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

95

from table1 left outer join table2 on

96

from table1 left outer join table2 on

97

from table1 left outer join table2 on

98

from table1 left outer join table2 on

99

from table1 left outer join table2 on

100

from table1 left outer join table2 on

101

from table1 left outer join table2 on

102

from table1 left outer join table2 on

103

British movie titles with director names

when available?

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'

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'

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'

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

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'

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

110

Which table will be fully shown?

111

Which table will be fully shown?

movies

credits

people

112

Which table will be fully shown?

movies

credits

people

113

Which table will be fully shown?

movies

credits

people

114

Which table will be fully shown?

movies

credits

people

Miss?

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

116

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

movies credits

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

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

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

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

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

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

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

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

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

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)

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)

128 128

129 129

130

Hugh Grant

130

131

Movies

132

Movies

People

133

Movies

People

Credits

134

Movies

People

Credits

movieid peopleid

135

Movies

People

Credits

movieid peopleid A

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)

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

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

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'

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'

141

Get movie titles and director name if available

142

Get movie titles and director name if available

Get movie titles and people involved, then display if

director known or no people found

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

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

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

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

147

Filter close to tables

148

Joins

149

Joins filtering

150

Joins

qualifying

filtering

151

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

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

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

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

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

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 = ...

157

Outer joins

158

Outer joins

qualifying

159

Outer joins filtering is null

160

Join

161

Join

162

Join MORE rows?

163

Join MORE rows?

filtering

164

Join MORE rows?

filtering

qualifying

165

166

filtering ASAP

167

filtering ASAP

168

filtering ASAP

qualifying later

169

filtering ASAP

qualifying later

170

MOVIES

170

171

US MOVIES

171

172

US MOVIES, 1940s

172

173

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

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

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

176

Set operators

177

UNION

178

US and GB movies, 1940s

179

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

US and GB movies, 1940s

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

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

182

183

184

185

186

movies

187

movies

premium_movies

188

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

189

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

190

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

all

191

current_year_data last_year_data

192

current_year_data last_year_data

193

current_year_data last_year_data

194

current_year_data last_year_data

195

current_year_data last_year_data

196

current_year_data current_year_data last_year_data

197

current_year_data current_year_data last_year_data

198

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

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

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

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

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

203

Goodfellas 2356

Goodfellas 2356

union

204

Goodfellas 2356

Goodfellas 2356

union

205

Goodfellas 2356

Goodfellas 2356

union all

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

207

208

union

209

intersect

210

except / minus

211

intersect

except

212

intersect

except

inner join

213

intersect

except

inner join

outer join

214

country codes that are both in movies and countries

215

select country_code from countries intersect select from movies

country

216

select country_code from countries intersect select from movies

distinct country

217

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

select distinct country from movies

218

select distinct country from movies

219

select country_code from countries except select distinct country from movies

220

select country_code from countries except select distinct country from movies

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

222

Equivalent columns

223

Equivalent columns

Flickr: Tomáš Obšívač

224

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

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

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

227

select * from movies where country_code = 'us'

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

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

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

231

232

233

234

235

236

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'

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'

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'

240

us

us

de in

gb

ru

us

in

241

us

us

de in

gb

ru

us

in

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

242

us

us

de in

gb

ru

us

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

243

us

us

de in

gb

ru

us

in

244

us

us

de in

gb

ru

us

in

245

us

us

de in

gb

ru

us

in

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

246

us

us

de in

gb

ru

us

in

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

247

us

us

de in

gb

ru

us

in

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

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'

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'

250

from clause

select list

where clause

251

from clause

select list

where clause

252

from clause

select list

where clause

253

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

254

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

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

255

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

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

256

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

257

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

258

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

259

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

260

in

distinct

261

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

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

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

264

no distinct Demonstrably unique

265

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

266

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

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

267

Explicit is better than

Implicit

268

duplicate rows?

269

duplicate rows?

nulls?

270

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

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

=

271

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

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

=

272

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

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

=

always

false

273

Augustus de Morgan (1806-1871)

274

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

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

=

275

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

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

=

always

false

276

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

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

=

277

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

278

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

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

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

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

282

from clause

select list

where clause

283

exists

284

exists

not exists

285

and exists (select ... ...)

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)

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)

288 Flickr: Kevin N. Murphy

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

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

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

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)

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

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

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

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

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

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

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)

300

correlated

uncorrelated

301

correlated

uncorrelated from

302

correlated

uncorrelated from

303

It depends ...

Flickr: Alan Cleaver 303

304

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

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.

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.

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.

top related