sql success ch03
DESCRIPTION
An SQL courseTRANSCRIPT
1
SQL Success
Chapter 3 slides
Stéphane Faroult
2
select * from tablename
3
select * from movies
4
5
All columns
6
variable1
variable2
variable3
Row Data
7
select movie_id, title, year_released, country_code from movies
8
select * from table
≈
print table
9
Restriction
10
Restriction
11
Restriction
12
Restriction
13
select * from movies
14
select * from movies
where ...
15
where country = 'us' select * from movies
16
where country = 'us' select * from movies
17
where country = 'us' select * from movies
18
where country = 'us' select * from movies
19
'constant'
20
column
21
Jaws where title =
22
Jaws where title =
23
'Jaws' where title =
24
Movies
25
Movies
26
US Movies
27
select * from movies where country = 'us'
28
select * from movies where country = 'us'
select * from (select * from movies where country = 'us') us_movies where year_released between 1940 and 1949
29
ALL MOVIES
29
30
US MOVIES
30
31
US MOVIES, 1940s
31
32
select * from (select * from movies where year_released between 1940 and 1949) movies_from_the_1940s where country = 'us'
33
select * from movies where country = 'us' and year_released between 1940 and 1949
34
select * from movies where country = 'us' and year_released between 1940 and 1949
or
35
select * from movies where country = 'us' and year_released between 1940 and 1949
or not
36
select * from movies where country = 'us' or country = 'gb'
37
and > or
38
and > or
2 + 3 * 4
39
and > or
2 + 3 * 4 3 * 4
40
and > or
2 + 3 * 4
12
3 * 4
41
2 + 12
and > or
2 + 3 * 4
12
3 * 4
42
2 + 12
and > or
2 + 3 * 4
14
43
where country = 'us' or country = 'gb' and year_released between 1940 and 1949
44
where country = 'us' or country = 'gb' and year_released between 1940 and 1949
country = 'gb' and year_released between 1940 and 1949
45
Movies
46
Movies
1940s
47
where (country = 'us' or country = 'gb') and year_released between 1940 and 1949
48
French movies from the 1940s
plus
American movies from the 1950s
49
select * from movies where (country = 'fr' and year_released between 1940 and 1949) or (country = 'us' and year_released between 1950 and 1959)
50
=
51
=
<> or !=
52
=
<= <
<> or !=
53
=
<= <
>
<> or !=
>=
54
2 < 10
55
2 < 10
'2' < '10'
56
2 < 10
'2' < '10'
57
2 < 10
'2' < '10'
'2-JUN-1883'>'1-DEC-2056'
58
2 < 10
'2' < '10'
'2-JUN-1883'>'1-DEC-2056'
As strings!
59
DD/MM/YYYY
60
DD/MM/YYYY
MM/DD/YYYY
61
Convert EXPLICITLY!
62
where issued = <some date>
63
where issued = <some date>
Flickr:Yoppy & Rudolf Schuba
64
where issued = <some date>
Flickr:Yoppy & Rudolf Schuba
65
66
67
where issued >= and issued <=
68
<Monday 00:00:00> <Friday 00:00:00>
where issued >= and issued <=
69
<Monday 00:00:00> <Friday 00:00:00>
where issued >= and issued <=
70
Sun Mon Tue Wed Thu Fri Sat
40
41
42
43
44
45
27 28 29 30 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
1 2 3 4 5 6 7
71
Sun Mon Tue Wed Thu Fri Sat
40
41
42
43
44
45
27 28 29 30 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
1 2 3 4 5 6 7
72
year_released between 1940 and 1949
73
year_released between 1940 and 1949
year_released >= 1940 and year_released <= 1949
74
where (country = 'us' or country = 'gb') and year_released between 1940 and 1949
75
where country in ('us', 'gb') and year_released between 1940 and 1949
where (country = 'us' or country = 'gb') and year_released between 1940 and 1949
76
country not in ('us', 'gb')
77
like
78
like
%
79
like
% _
80
select * from movies where title not like '%A%' and title not like '%a%'
81
select * from movies where upper(title) not like '%A%'
82
select * from movies where upper(title) not like '%A%'
83 Flickr:Daniel Moyle 83
84
if (ptr == NULL) {
…
85
if (ptr == NULL) {
…
86
NULL in SQL is NOT a value …
87
where column_name = null
88
where column_name = null
89
where column_name <> null
90
where column_name <> null
91
where column_name is null
92
where column_name is null
where column_name is not null
93
Who are the people in the database who are alive?
94
95
96
select title, year_released from movies where country = 'us'
97
databases - +
98
databases -
schemas +
99
databases -
schemas +
tables +
-
100
databases -
schemas +
tables +
columns +
-
-
101
desc movies;
102
desc movies;
describe table movies
103
desc movies;
describe table movies
\d movies
104
desc movies;
describe table movies
\d movies
.schema movies
105
compute
106
compute
derive
107 Flickr: Etsuko Nakamura
108 Flickr: Etsuko Nakamura
109
'hello' ' world' +
110
'hello' ' world' ||
111
concat( , ) 'hello' ' world'
112
select title || ' was released in ' || year_released movie_release from movies where country = 'us'
113
select title || ' was released in ' || year_released movie_release from movies where country = 'us'
114
select title + ' was released in ' + cast(year_released as varchar) movie_release from movies where country = 'us'
115
select concat(title, ' was released in ', year_released) movie_release from movies where country = 'us'
116
people
Age of people alive?
117
people born died
Age of people alive?
118
Alive
119
died
Alive is null
120
died
Alive is null
Age
121
died
Alive is null
Age born <this year> -
122
select from some_table where some_column = some_user_input
column1, ...
123
select from some_table where some_column = some_user_input
f(column1), ...
124
select from some_table where some_column = some_user_input
f(column1), ...
125
select from some_table where some_column = f(some_user_input)
column1, ...
126
select from some_table where f(some_column) = some_user_input
column1, ...
127 Flickr:Tony Austin
case end
128
Y
n
color case upper(color) when 'Y' then 'Color' when 'N' then 'B&W' else '?' end as color, ...
129
Y
n
color case upper(color) when 'Y' then 'Color' when 'N' then 'B&W' else '?' end as color, ...
130
Y
n
color case upper(color) when 'Y' then 'Color' when 'N' then 'B&W' else '?' end as color, ...
Color
131
case upper(color) when 'Y' then 'Color' when 'N' then 'B&W' else '?' end as color, ...
Y
n
color
Color
132
case upper(color) when 'Y' then 'Color' when 'N' then 'B&W' else '?' end as color, ...
Y
n
color
Color
?
133
case upper(color) when 'Y' then 'Color' when 'N' then 'B&W' else '?' end as color, ...
Y
n
color
Color
B&W
?
134
case upper(color) when 'Y' then 'Color' when 'N' then 'B&W' else '?' end as color, ...
Y
n
color
Color
B&W
?
135
136
case column_name when null then end
137
case column_name when null then end
138
case upper(color) when 'Y' then 'Color' when 'N' then 'B&W' else '?' end as color,
139
when 1929 then 'passed away'
case died when 1920 then 'passed away' when 1921 then 'passed away'
when 1922 then 'passed away'
when 1923 then 'passed away'
when 1924 then 'passed away'
when 1925 then 'passed away'
when 1926 then 'passed away'
when 1927 then 'passed away'
when 1928 then 'passed away'
140
case when died is null then 'alive and kicking' else 'passed away' end as status
died is null
141
Flickr:Sanath Kumar
Some useful functions
more in appendix B ...
142
round( )
143
round(
trunc(
)
)
144
round(
trunc( )
3 3.141592, 0)
145
round(
trunc( 3.141592)
3
3
3.141592, 0)
146
3.141592, 3) round(
trunc( 3.141592)
3.142
3
147
3.141592, 3)
3.141592, 3) round(
trunc(
3.142
3.141
148
upper(), lower()
149
substr(
upper(), lower()
)
150
substr(
upper(), lower()
'Citizen Kane', 5, 3)
151
substr(
trim(
upper(), lower()
)
)
152
substr(
trim(
upper(), lower()
)
' Oops ') 'Oops'
153
substr(
trim(
replace(
upper(), lower()
)
)
)
154
'Sheep', 'ee', 'i')
substr(
trim(
replace(
upper(), lower()
)
)
'Ship'
155
upper(), lower()
156
substring(
upper(), lower()
)
157
substring(
upper(), lower()
'Citizen Kane', 5, 3)
158
substring(
ltrim(
upper(), lower()
)
)
159
substring(
ltrim(
upper(), lower()
)
' Oops ') 'Oops '
160
substring(
ltrim(
upper(), lower()
)
' Oops ') 'Oops '
rtrim()
161
substring(
ltrim(
replace(
upper(), lower()
)
)
)
rtrim()
162
'Sheep', 'ee', 'i')
substring(
ltrim(
replace(
upper(), lower()
)
)
rtrim()
'Ship'
163
Current date
164
MARCH FEBRUARY
Current date
Date Arithmetic
165
MARCH
Current date
Date Arithmetic
+ 1 month
+ 30 days
166
dateadd(month, 1, date_col)
167
dateadd(month, 1, date_col)
date_col + 1 month
168
dateadd(month, 1, date_col)
date_col + 1 month
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)
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)
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
172
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')
173
'28-DEC-1895'
174
'28-DEC-1895'
175
'28-DEC-1895'
DBMS date
176
'12/28/1895'
DBMS date
177
178
cast( as )
179
y z x
180
(x + y)
z
181
(x + y)
z
182
No duplicates
183
No duplicates
Identifier
184
select country from movies
185
186
187
distinct
188
distinct
select distinct country from movies
189
Aggregate functions
190
select country, year_released, title from movies
191
us 1942 Casablanca
us 1990 Goodfellas
ru 1925 Bronenosets Potyomkin
us 1982 Blade Runner
us 1977 Annie Hall
hk 1986 Ying hung boon sik
in 1975 Sholay
us 1954 On The Waterfront
gb 1962 Lawrence Of Arabia
gb 1949 The Third Man
it 1948 Ladri di biciclette
us 1941 Citizen Kane
de 1985 Das Boot
se 1957 Det sjunde inseglet
fr 1997 Le cinquième élément
it 1966 Il buono, il brutto, il cattivo
jp 1954 Shichinin no Samurai
in 1955 Pather Panchali
nz 2001 The Lord of the Rings
fr 1946 La belle et la bête
...
select country, year_released, title from movies
192
de 1985 Das Boot
fr 1997 Le cinquième élément
fr 1946 La belle et la bête
fr 1942 Les Visiteurs du Soir
gb 1962 Lawrence Of Arabia
gb 1949 The Third Man
hk 1986 Ying hung boon sik
in 1975 Sholay
in 1955 Pather Panchali
it 1948 Ladri di biciclette
it 1966 Il buono, il brutto, il cattivo
jp 1954 Shichinin no Samurai
nz 2001 The Lord of the Rings
ru 1925 Bronenosets Potyomkin
se 1957 Det sjunde inseglet
us 1942 Casablanca
us 1990 Goodfellas
us 1982 Blade Runner
us 1977 Annie Hall
us 1954 On The Waterfront
...
select country, year_released, title from movies
193
de 1985 Das Boot
fr 1997 Le cinquième élément
fr 1946 La belle et la bête
fr 1942 Les Visiteurs du Soir
gb 1962 Lawrence Of Arabia
gb 1949 The Third Man
hk 1986 Ying hung boon sik
in 1975 Sholay
in 1955 Pather Panchali
it 1948 Ladri di biciclette
it 1966 Il buono, il brutto, il cattivo
jp 1954 Shichinin no Samurai
nz 2001 The Lord of the Rings
ru 1925 Bronenosets Potyomkin
se 1957 Det sjunde inseglet
us 1942 Casablanca
us 1990 Goodfellas
us 1982 Blade Runner
us 1977 Annie Hall
us 1954 On The Waterfront
...
select country, year_released, title from movies
194
group by
195
group by
select country, count(*) number_of_movies from movies group by country
count(*)
196
group by
select country, count(*) number_of_movies from movies group by country
country
197
group by
select country, count(*) number_of_movies from movies group by country
country
198
de 1985 Das Boot
fr 1997 Le cinquième élément
fr 1946 La belle et la bête
fr 1942 Les Visiteurs du Soir
gb 1962 Lawrence Of Arabia
gb 1949 The Third Man
hk 1986 Ying hung boon sik
in 1975 Sholay
in 1955 Pather Panchali
it 1948 Ladri di biciclette
it 1966 Il buono, il brutto, il cattivo
jp 1954 Shichinin no Samurai
nz 2001 The Lord of the Rings
ru 1925 Bronenosets Potyomkin
se 1957 Det sjunde inseglet
us 1942 Casablanca
us 1990 Goodfellas
us 1982 Blade Runner
us 1977 Annie Hall
us 1954 On The Waterfront
...
select country, year_released, title from movies
1
3
2
1
2
2
1 1
1 1
17
199
select country, year_released, count(*) number_of_movies from movies group by country, year_released
200
select country, year_released, count(*) number_of_movies from movies group by country, year_released
201
select count(*) number_of_movies from movies
202
where
203
where
204
where
205
where
206
where
207
distinct, group by
208
distinct, group by
209
distinct, group by
210
distinct, group by
211
distinct, group by
212
count(*)
min(
max(
avg(
count(col)
col)
col)
col)
213
Earliest release year by country?
214
select country, min(year_released) oldest_movie from movies group by country
Earliest release year by country?
215
select country, min(year_released) oldest_movie from movies group by country
216
select country, min(year_released) oldest_movie from movies group by country
select * from ( ) earliest_movies_per_country where oldest_movie < 1940
217
select country, min(year_released) oldest_movie from movies group by country
having
218
select country, min(year_released) oldest_movie from movies group by country
having min(year_released) < 1940
having
219 Flickr: Randy Robertson
SORT
219
220
group by country having country = 'us'
select country, min(year_released) oldest_movie from movies
221
where country = 'us' group by country
select country, min(year_released) oldest_movie from movies
222 Flickr: Dano 222
223
having
Result of aggregate
224
Nulls?
225
Nulls?
known + unknown = unknown
226
FLickr: Linda Åslund
Aggregate functions
ignore Nulls
226
227
select max(died) most_recent_death from people
228
select max(died) most_recent_death from people where died is not null
229
count(*)
230
count(*)
231
count(col)
232
count(col)
233
select count(*) people_count, count(born) birth_year_count, count(died) death_year_count from people
234
select count(colname)
235
select count(distinct colname)
236
select country, count(distinct year_released) number_of_years from movies group by country
237
select country, count(*) number_of_years from (select distinct country, year_released from movies) t group by country
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?
245
How many people are both
actors and directors?
credits
246
8 37 D 8 38 A 8 39 A 8 40 A 10 11 A 10 12 A 10 15 D 10 16 A 10 17 A 12 11 A 12 11 D 12 12 A 136 378 D 136 433 A 136 434 A 136 435 A 115 38 A 115 359 D 115 360 A ...
movieid peopleid credited_as
247
8 37 D 8 38 A 8 39 A 8 40 A 10 11 A 10 12 A 10 15 D 10 16 A 10 17 A 12 11 A 12 11 D 12 12 A 136 378 D 136 433 A 136 434 A 136 435 A 115 38 A 115 359 D 115 360 A ...
movieid peopleid credited_as
select peopleid, credited_as from credits
248
8 37 D 8 38 A 8 39 A 8 40 A 10 11 A 10 12 A 10 15 D 10 16 A 10 17 A 12 11 A 12 11 D 12 12 A 136 378 D 136 433 A 136 434 A 136 435 A 115 38 A 115 359 D 115 360 A ...
movieid peopleid credited_as
38 A
38 A
select peopleid, credited_as from credits
249
8 37 D 8 38 A 8 39 A 8 40 A 10 11 A 10 12 A 10 15 D 10 16 A 10 17 A 12 11 A 12 11 D 12 12 A 136 378 D 136 433 A 136 434 A 136 435 A 115 38 A 115 359 D 115 360 A ...
movieid peopleid credited_as
38 A
38 A
11 A
11 A
select peopleid, credited_as from credits
250
peopleid credited_as
11 A 11 D 12 A 15 D 16 A 17 A 37 D 38 A 39 A 40 A 359 D 360 A 361 A 378 D 379 A 380 A 442 A 442 D 443 A ...
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')
252
( ) all_actors_and_directors
select distinct peopleid, credited_as from credits where credited_as in ('A', 'D')
253
select peopleid, count(*) as number_of_roles from group by peopleid having count(*) = 2
( ) all_actors_and_directors
select distinct peopleid, credited_as from credits where credited_as in ('A', 'D')
254
select peopleid, count(*) as number_of_roles from group by peopleid having count(*) = 2
( ) all_actors_and_directors
select distinct peopleid, credited_as from credits where credited_as in ('A', 'D')
select count(*) number_of_acting_directors from (
) acting_directors
255
Rows are selected with where + conditions. Beware of or. Beware of nulls. Beware of dates.
256
Rows are selected with where + conditions. Beware of or. Beware of nulls. Beware of dates. Many functions. case ... end for conditional display.
257
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.