SQL Success
Chapter 3 slides
Stéphane Faroult

1
select * from tablename

2
select * from movies

3
4
All columns
5
Row Data
variable1

variable2

variable3
6
select movie_id,
title,
year_released,
country_code
from movies
7
select * from table

≈
print table

8
Restriction

9
Restriction

10
Restriction

11
Restriction

12
select * from movies

13
select * from movies
where ...

14
select * from movies
where country = 'us'

15
select * from movies
where country = 'us'

16
select * from movies
where country = 'us'

17
select * from movies
where country = 'us'

18
'constant'
19
column
20
where title = Jaws

21
where title = Jaws

22
where title ='Jaws'

23
Movies

24
Movies

25
US Movies

26
select * from movies
where country = 'us'

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

28
ALL MOVIES
29
US MOVIES
30
US MOVIES, 1940s
31
select *
from (select * from movies
where year_released between 1940
and 1949)
movies_from_the_1940s
where country = 'us'

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

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

or
34
select *
from movies
where country = 'us'
and year_released between 1940 and 1949

or

not
35
select * from movies
where country = 'us'
or country = 'gb'

36
and > or

37
and > or

2+3*4

38
and > or

2+3*4

39
and > or

2+3*4
12
40
and > or

2+3*4
2 + 12
41
and > or

2+3*4
2 + 12
14

42
where country = 'us'
or country = 'gb'
and year_released between 1940
and 1949

43
where country = 'us'
or country = 'gb'
and year_released between 1940
and 1949

44
Movies

45
Movies

1940s

46
where (country = 'us'
or country = 'gb')
and year_released between 1940
and 1949

47
French movies from the 1940s
plus
American movies from the 1950s
48
select * from movies
where (country = 'fr'
and year_released between
and
or (country = 'us'
and year_released between
and

1940
1949)
1950
1959)

49
=

50
=
<> or !=

51
=
<> or !=
<
<=
52
=
<> or !=
<
<=
>
>=
53
2 < 10

54
2 < 10
'2' < '10'

55
2 < 10
'2' < '10'

56
2 < 10
'2' < '10'
'2-JUN-1883'>'1-DEC-2056'

57
2 < 10
'2' < '10'
'2-JUN-1883'>'1-DEC-2056'

As strings!
58
DD/MM/YYYY

59
DD/MM/YYYY
MM/DD/YYYY
60
Convert
EXPLICITLY!
61
where issued = <some date>

62
where issued = <some date>

Flickr:Yoppy & Rudolf Schuba

63
where issued = <some date>

Flickr:Yoppy & Rudolf Schuba

64
65
66
where issued >=
and issued <=

67
where issued >= <Monday 00:00:00>
and issued <= <Friday 00:00:00>

68
where issued >= <Monday 00:00:00>
and issued <= <Friday 00:00:00>

69
40
41
42
43
44
45

Sun Mon Tue Wed Thu
27 28 29 30
1
4
5
6
7
8
11 12 13 14 15
18 19 20 21 22
25 26 27 28 29
1
2
3
4
5

Fri
2
9
16
23
30
6

Sat
3
10
17
24
31
7

70
40
41
42
43
44
45

Sun Mon Tue Wed Thu
27 28 29 30
1
4
5
6
7
8
11 12 13 14 15
18 19 20 21 22
25 26 27 28 29
1
2
3
4
5

Fri
2
9
16
23
30
6

Sat
3
10
17
24
31
7

71
year_released between 1940 and 1949

72
year_released between 1940 and 1949
year_released >= 1940
and year_released <= 1949

73
where (country = 'us'
or country = 'gb')
and year_released between 1940 and 1949

74
where (country = 'us'
or country = 'gb')
and year_released between 1940 and 1949

where country in ('us', 'gb')
and year_released between 1940 and 1949

75
country not in ('us', 'gb')

76
like

77
like

%
78
like

%

_
79
select * from movies
where title not like '%A%'
and title not like '%a%'

80
select * from movies
where upper(title)
not like '%A%'

81
select * from movies
where upper(title)
not like '%A%'

82
Flickr:Daniel Moyle

83
if (ptr == NULL) {
…

84
if (ptr == NULL) {
…

85
NULL in SQL
is NOT a value …
86
where column_name = null

87
where column_name = null

88
where column_name <> null

89
where column_name <> null

90
where column_name is null

91
where column_name is null
where column_name is not null

92
Who are the people
in the database
who are alive?
93
94
95
select title, year_released
from movies
where country = 'us'

96
+
-

databases

97
-

databases
+

schemas

98
-

databases
+
-

schemas
+

tables

99
-

databases
+
-

schemas
+
-

tables
+

columns

100
desc movies;

101
desc movies;
describe table movies

102
desc movies;
describe table movies
d movies

103
desc movies;
describe table movies
d movies
.schema movies
104
compute

105
compute

derive
106
Flickr: Etsuko Nakamura

107
Flickr: Etsuko Nakamura

108
'hello' + ' world'

109
'hello' ||' world'

110
concat('hello' , ' world')

111
select title
|| ' was released in '
|| year_released movie_release
from movies
where country = 'us'

112
select title
|| ' was released in '
|| year_released movie_release
from movies
where country = 'us'

113
select title
+ ' was released in '
+ cast(year_released
as varchar) movie_release
from movies
where country = 'us'

114
select concat(title,
' was released in ',
year_released) movie_release
from movies
where country = 'us'

115
people

Age of people alive?
116
people

born
died

Age of people alive?
117
Alive

118
Alive
died is null

119
Alive
died is null

Age
120
Alive
died is null

Age
<this year> - born

121
select column1, ...
from some_table
where some_column = some_user_input

122
select f(column1), ...
from some_table
where some_column = some_user_input

123
select f(column1), ...
from some_table
where some_column = some_user_input

124
select column1, ...
from some_table
where some_column = f(some_user_input)

125
select column1, ...
from some_table
where f(some_column) = some_user_input

126
case
end

Flickr:Tony Austin

127
color

Y
n

case upper(color)
when 'Y' then 'Color'
when 'N' then 'B&W'
else '?'
end as color,
...

128
color

Y
n

case upper(color)
when 'Y' then 'Color'
when 'N' then 'B&W'
else '?'
end as color,
...

129
color

Y
n

Color

case upper(color)
when 'Y' then 'Color'
when 'N' then 'B&W'
else '?'
end as color,
...

130
color

Y
n

Color

case upper(color)
when 'Y' then 'Color'
when 'N' then 'B&W'
else '?'
end as color,
...

131
color

Y

Color
?

n

case upper(color)
when 'Y' then 'Color'
when 'N' then 'B&W'
else '?'
end as color,
...

132
color

Y

Color
?

n

B&W

case upper(color)
when 'Y' then 'Color'
when 'N' then 'B&W'
else '?'
end as color,
...

133
color

Y

Color
?

n

B&W

case upper(color)
when 'Y' then 'Color'
when 'N' then 'B&W'
else '?'
end as color,
...

134
135
case column_name
when null then
end

136
case column_name
when null then
end

137
case upper(color)
when 'Y' then 'Color'
when 'N' then 'B&W'
else '?'
end as color,

138
case died
when 1920
when 1921
when 1922
when 1923
when 1924
when 1925
when 1926
when 1927
when 1928

then
then
then
then
then
then
then
then
then

'passed
'passed
'passed
'passed
'passed
'passed
'passed
'passed
'passed

away'
away'
away'
away'
away'
away'
away'
away'
away'
139
case
when died is
'alive
else 'passed
end as status

null then
and kicking'
away'

140
Some useful
functions
more in appendix B ...
Flickr:Sanath Kumar

141
round()

142
round()
trunc()

143
round(3.141592, 0)

3

trunc()

144
round(3.141592, 0)

3

trunc(3.141592)

3

145
round(3.141592, 3)

3.142

trunc(3.141592)

3

146
round(3.141592, 3)

3.142

trunc(3.141592, 3)

3.141

147
upper(), lower()

148
upper(), lower()
substr()

149
upper(), lower()
substr('Citizen Kane', 5, 3)

150
upper(), lower()
substr()
trim( )

151
upper(), lower()
substr()
trim( ' Oops

')

'Oops'

152
upper(), lower()
substr()
trim( )

replace()

153
upper(), lower()
substr()
trim( )

replace('Sheep', 'ee', 'i')
'Ship'
154
upper(), lower()

155
upper(), lower()
substring()

156
upper(), lower()

substring('Citizen Kane', 5, 3)

157
upper(), lower()
substring()
ltrim()

158
upper(), lower()
substring()
ltrim(' Oops

')

'Oops

'

159
upper(), lower()
substring()
ltrim(' Oops

')

'Oops

'

rtrim()

160
upper(), lower()
substring()
ltrim()

rtrim()
replace()
161
upper(), lower()
substring()
ltrim()

rtrim()
replace('Sheep', 'ee', 'i')
'Ship'
162
Current date

163
Current date
Date Arithmetic
FEBRUARY
MARCH

164
Current date
Date Arithmetic
MARCH

+ 1 month
+ 30 days
165
dateadd(month, 1, date_col)

166
dateadd(month, 1, date_col)
date_col + 1 month

167
dateadd(month, 1, date_col)
date_col + 1 month
date_col + interval'1 month'

168
dateadd(month, 1, date_col)
date_col + 1 month
date_col + interval'1 month'
date_add(date_col, interval 1 month)

169
dateadd(month, 1, date_col)
date_col + 1 month
date_col + interval'1 month'
date_add(date_col, interval 1 month)
add_months(date_col, 1)

170
dateadd(month, 1, date_col)
date_col + 1 month
date_col + interval'1 month'
date_add(date_col, interval 1 month)
add_months(date_col, 1)
date_col + decimal_number

171
dateadd(month, 1, date_col)
date_col + 1 month
date_col + interval'1 month'
date_add(date_col, interval 1 month)
add_months(date_col, 1)
date_col + decimal_number
date(date_col, '1 month')
172
'28-DEC-1895'

173
'28-DEC-1895'

174
'28-DEC-1895'

DBMS date

175
'12/28/1895'

DBMS date

176
177
cast(

as

)

178
x y z

179
z

(x

+

y)
180
z

(x

+

y)
181
No duplicates

182
No duplicates
Identifier

183
select country from movies

184
185
186
distinct

187
distinct
select distinct country
from movies

188
Aggregate functions

189
select country, year_released, title
from movies

190
select country, year_released, title
from movies
us
us
ru
us
us
hk
in
us
gb
gb
it
us
de
se
fr
it
jp
in
nz
fr
...

1942
1990
1925
1982
1977
1986
1975
1954
1962
1949
1948
1941
1985
1957
1997
1966
1954
1955
2001
1946

Casablanca
Goodfellas
Bronenosets Potyomkin
Blade Runner
Annie Hall
Ying hung boon sik
Sholay
On The Waterfront
Lawrence Of Arabia
The Third Man
Ladri di biciclette
Citizen Kane
Das Boot
Det sjunde inseglet
Le cinquième élément
Il buono, il brutto, il cattivo
Shichinin no Samurai
Pather Panchali
The Lord of the Rings
La belle et la bête

191
select country, year_released, title
from movies
de
fr
fr
fr
gb
gb
hk
in
in
it
it
jp
nz
ru
se
us
us
us
us
us
...

1985
1997
1946
1942
1962
1949
1986
1975
1955
1948
1966
1954
2001
1925
1957
1942
1990
1982
1977
1954

Das Boot
Le cinquième élément
La belle et la bête
Les Visiteurs du Soir
Lawrence Of Arabia
The Third Man
Ying hung boon sik
Sholay
Pather Panchali
Ladri di biciclette
Il buono, il brutto, il cattivo
Shichinin no Samurai
The Lord of the Rings
Bronenosets Potyomkin
Det sjunde inseglet
Casablanca
Goodfellas
Blade Runner
Annie Hall
On The Waterfront

192
select country, year_released, title
from movies
de
fr
fr
fr
gb
gb
hk
in
in
it
it
jp
nz
ru
se
us
us
us
us
us
...

1985
1997
1946
1942
1962
1949
1986
1975
1955
1948
1966
1954
2001
1925
1957
1942
1990
1982
1977
1954

Das Boot
Le cinquième élément
La belle et la bête
Les Visiteurs du Soir
Lawrence Of Arabia
The Third Man
Ying hung boon sik
Sholay
Pather Panchali
Ladri di biciclette
Il buono, il brutto, il cattivo
Shichinin no Samurai
The Lord of the Rings
Bronenosets Potyomkin
Det sjunde inseglet
Casablanca
Goodfellas
Blade Runner
Annie Hall
On The Waterfront

193
group by

194
group by
select country,
count(*) number_of_movies
from movies
group by country
195
group by
select country
country,
count(*) number_of_movies
from movies
group by country
196
group by
select country
country,
count(*) number_of_movies
from movies
group by country
197
select country, year_released, title
from movies
de
fr
fr
fr
gb
gb
hk
in
in
it
it
jp
nz
ru
se
us
us
us
us
us
...

1985
1997
1946
1942
1962
1949
1986
1975
1955
1948
1966
1954
2001
1925
1957
1942
1990
1982
1977
1954

Das Boot
Le cinquième élément
La belle et la bête
Les Visiteurs du Soir
Lawrence Of Arabia
The Third Man
Ying hung boon sik
Sholay
Pather Panchali
Ladri di biciclette
Il buono, il brutto, il cattivo
Shichinin no Samurai
The Lord of the Rings
Bronenosets Potyomkin
Det sjunde inseglet
Casablanca
Goodfellas
Blade Runner
Annie Hall
On The Waterfront

1
3
2
1
2
2
1
1

1
1

17
198
select country,
year_released,
count(*) number_of_movies
from movies
group by country,
year_released

199
select country,
year_released,
count(*) number_of_movies
from movies
group by country,
year_released

200
select count(*) number_of_movies
from movies

201
where

202
where

203
where

204
where

205
where

206
distinct, group by

207
distinct, group by

208
distinct, group by

209
distinct, group by

210
distinct, group by

211
count(*) count(col)
min(col)
max(col)

avg(col)
212
Earliest release year
by country?

213
Earliest release year
by country?
select country,
min(year_released) oldest_movie
from movies
group by country

214
select country,
min(year_released) oldest_movie
from movies
group by country

215
select *
from (
select country,
min(year_released) oldest_movie
from movies
group by country
) earliest_movies_per_country
where oldest_movie < 1940

216
having
select country,
min(year_released) oldest_movie
from movies
group by country

217
having
select country,
min(year_released) oldest_movie
from movies
group by country
having min(year_released) < 1940

218
SORT

Flickr: Randy Robertson

219
select country,
min(year_released) oldest_movie
from movies
group by country
having country = 'us'

220
select country,
min(year_released) oldest_movie
from movies
where country = 'us'
group by country

221
Flickr: Dano

222
having

Result of aggregate
223
Nulls?

224
Nulls?
known + unknown = unknown

225
Aggregate functions

ignore
Nulls

FLickr: Linda Åslund

226
select max(died) most_recent_death
from people

227
select max(died) most_recent_death
from people
where died is not null

228
count(*)

229
count(*)

230
count(col)

231
count(col)

232
select count(*) people_count,
count(born) birth_year_count,
count(died) death_year_count
from people

233
select count(colname)

234
select count(distinct colname)

235
select country,
count(distinct year_released)
number_of_years
from movies
group by country

236
select country,
count(*) number_of_years
from (select distinct country,
year_released
from movies) t
group by country

237
How many people
are both
actors and directors?

238
How many people
are both
actors and directors?

239
How many people
are both
actors and directors?

240
How many people
are both
actors and directors?

241
How many people
are both
actors and directors?

242
How many people
are both
actors and directors?

243
How many people
are both
actors and directors?

244
How many people
are both
actors and directors?

credits
245
movieid

peopleid

8
8
8
8
10
10
10
10
10
12
12
12
136
136
136
136
115
115
115
...

37
38
39
40
11
12
15
16
17
11
11
12
378
433
434
435
38
359
360

credited_as

D
A
A
A
A
A
D
A
A
A
D
A
D
A
A
A
A
D
A
246
movieid

peopleid

8
8
8
8
10
10
10
10
10
12
12
12
136
136
136
136
115
115
115
...

37
38
39
40
11
12
15
16
17
11
11
12
378
433
434
435
38
359
360

credited_as

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

select peopleid,
credited_as
from credits

247
movieid

peopleid

8
8
8
8
10
10
10
10
10
12
12
12
136
136
136
136
115
115
115
...

37
38
39
40
11
12
15
16
17
11
11
12
378
433
434
435
38
359
360

credited_as

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

select peopleid,
credited_as
from credits

248
movieid

peopleid

8
8
8
8
10
10
10
10
10
12
12
12
136
136
136
136
115
115
115
...

37
38
39
40
11
12
15
16
17
11
11
12
378
433
434
435
38
359
360

credited_as

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

select peopleid,
credited_as
from credits

249
peopleid

11
11
12
15
16
17
37
38
39
40
359
360
361
378
379
380
442
442
443
...

credited_as

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

select distinct
peopleid,
credited_as
from credits
where credited_as
in ('A', 'D')

250
select distinct
peopleid,
credited_as
from credits
where credited_as
in ('A', 'D')

251
( select distinct
peopleid,
credited_as
from credits
where credited_as
in ('A', 'D')) all_actors_and_directors

252
select peopleid, count(*) as number_of_roles
from ( select distinct
peopleid,
credited_as
from credits
where credited_as
in ('A', 'D')) all_actors_and_directors
group by peopleid
having count(*) = 2

253
select count(*) number_of_acting_directors
from (
select peopleid, count(*) as number_of_roles
from ( select distinct
peopleid,
credited_as
from credits
where credited_as
in ('A', 'D')) all_actors_and_directors
group by peopleid
having count(*) = 2 ) acting_directors

254
Rows are selected with where + conditions.
Beware of or.
Beware of nulls.
Beware of dates.

255
Rows are selected with where + conditions.
Beware of or.
Beware of nulls.
Beware of dates.
Many functions. case ... end for conditional
display.

256
Rows are selected with where + conditions.
Beware of or.
Beware of nulls.
Beware of dates.
Many functions. case ... end for conditional
display.
You must use distinct or group by to remove
duplicates from a result set if there is no key.

257

Sql success ch03