This document contains slides from a chapter on SQL success. It covers various SQL SELECT statements with different clauses like WHERE, BETWEEN, IN, LIKE, NULL, CASE, functions, and aggregates. It discusses selecting columns, filtering rows, string operations, date functions, and grouping or aggregating data. The slides provide examples of selecting movies by country and year, concatenating strings, calculating ages from dates, and using CASE and DISTINCT.
74. where (country = 'us'
or country = 'gb')
and year_released between 1940 and 1949
74
75. 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
139. 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
191. 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
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
192
193. 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
198. 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
216. select *
from (
select country,
min(year_released) oldest_movie
from movies
group by country
) earliest_movies_per_country
where oldest_movie < 1940
216
253. 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
254. 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
255. Rows are selected with where + conditions.
Beware of or.
Beware of nulls.
Beware of dates.
255
256. Rows are selected with where + conditions.
Beware of or.
Beware of nulls.
Beware of dates.
Many functions. case ... end for conditional
display.
256
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.
257