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

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...
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...
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_r...
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
t...
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_month...
dateadd(month, 1, date_col)
date_col + 1 month
date_col + interval'1 month'
date_add(date_col, interval 1 month)
add_month...
dateadd(month, 1, date_col)
date_col + 1 month
date_col + interval'1 month'
date_add(date_col, interval 1 month)
add_month...
'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
199...
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
199...
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
199...
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
199...
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...
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
37...
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
37...
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
37...
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
37...
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
...
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
i...
select count(*) number_of_acting_directors
from (
select peopleid, count(*) as number_of_roles
from ( select distinct
peop...
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 fo...
Rows are selected with where + conditions.
Beware of or.
Beware of nulls.
Beware of dates.
Many functions. case ... end fo...
Upcoming SlideShare
Loading in...5
×

Sql success ch03

103
-1

Published on

An SQL course

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
103
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Sql success ch03

  1. 1. SQL Success Chapter 3 slides Stéphane Faroult 1
  2. 2. select * from tablename 2
  3. 3. select * from movies 3
  4. 4. 4
  5. 5. All columns 5
  6. 6. Row Data variable1 variable2 variable3 6
  7. 7. select movie_id, title, year_released, country_code from movies 7
  8. 8. select * from table ≈ print table 8
  9. 9. Restriction 9
  10. 10. Restriction 10
  11. 11. Restriction 11
  12. 12. Restriction 12
  13. 13. select * from movies 13
  14. 14. select * from movies where ... 14
  15. 15. select * from movies where country = 'us' 15
  16. 16. select * from movies where country = 'us' 16
  17. 17. select * from movies where country = 'us' 17
  18. 18. select * from movies where country = 'us' 18
  19. 19. 'constant' 19
  20. 20. column 20
  21. 21. where title = Jaws 21
  22. 22. where title = Jaws 22
  23. 23. where title ='Jaws' 23
  24. 24. Movies 24
  25. 25. Movies 25
  26. 26. US Movies 26
  27. 27. select * from movies where country = 'us' 27
  28. 28. select * from (select * from movies select where country = 'us' us_movies 'us') where year_released between 1940 and 1949 28
  29. 29. ALL MOVIES 29
  30. 30. US MOVIES 30
  31. 31. US MOVIES, 1940s 31
  32. 32. select * from (select * from movies where year_released between 1940 and 1949) movies_from_the_1940s where country = 'us' 32
  33. 33. select * from movies where country = 'us' and year_released between 1940 and 1949 33
  34. 34. select * from movies where country = 'us' and year_released between 1940 and 1949 or 34
  35. 35. select * from movies where country = 'us' and year_released between 1940 and 1949 or not 35
  36. 36. select * from movies where country = 'us' or country = 'gb' 36
  37. 37. and > or 37
  38. 38. and > or 2+3*4 38
  39. 39. and > or 2+3*4 39
  40. 40. and > or 2+3*4 12 40
  41. 41. and > or 2+3*4 2 + 12 41
  42. 42. and > or 2+3*4 2 + 12 14 42
  43. 43. where country = 'us' or country = 'gb' and year_released between 1940 and 1949 43
  44. 44. where country = 'us' or country = 'gb' and year_released between 1940 and 1949 44
  45. 45. Movies 45
  46. 46. Movies 1940s 46
  47. 47. where (country = 'us' or country = 'gb') and year_released between 1940 and 1949 47
  48. 48. French movies from the 1940s plus American movies from the 1950s 48
  49. 49. 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. 50. = 50
  51. 51. = <> or != 51
  52. 52. = <> or != < <= 52
  53. 53. = <> or != < <= > >= 53
  54. 54. 2 < 10 54
  55. 55. 2 < 10 '2' < '10' 55
  56. 56. 2 < 10 '2' < '10' 56
  57. 57. 2 < 10 '2' < '10' '2-JUN-1883'>'1-DEC-2056' 57
  58. 58. 2 < 10 '2' < '10' '2-JUN-1883'>'1-DEC-2056' As strings! 58
  59. 59. DD/MM/YYYY 59
  60. 60. DD/MM/YYYY MM/DD/YYYY 60
  61. 61. Convert EXPLICITLY! 61
  62. 62. where issued = <some date> 62
  63. 63. where issued = <some date> Flickr:Yoppy & Rudolf Schuba 63
  64. 64. where issued = <some date> Flickr:Yoppy & Rudolf Schuba 64
  65. 65. 65
  66. 66. 66
  67. 67. where issued >= and issued <= 67
  68. 68. where issued >= <Monday 00:00:00> and issued <= <Friday 00:00:00> 68
  69. 69. where issued >= <Monday 00:00:00> and issued <= <Friday 00:00:00> 69
  70. 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 70
  71. 71. 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
  72. 72. year_released between 1940 and 1949 72
  73. 73. year_released between 1940 and 1949 year_released >= 1940 and year_released <= 1949 73
  74. 74. where (country = 'us' or country = 'gb') and year_released between 1940 and 1949 74
  75. 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
  76. 76. country not in ('us', 'gb') 76
  77. 77. like 77
  78. 78. like % 78
  79. 79. like % _ 79
  80. 80. select * from movies where title not like '%A%' and title not like '%a%' 80
  81. 81. select * from movies where upper(title) not like '%A%' 81
  82. 82. select * from movies where upper(title) not like '%A%' 82
  83. 83. Flickr:Daniel Moyle 83
  84. 84. if (ptr == NULL) { … 84
  85. 85. if (ptr == NULL) { … 85
  86. 86. NULL in SQL is NOT a value … 86
  87. 87. where column_name = null 87
  88. 88. where column_name = null 88
  89. 89. where column_name <> null 89
  90. 90. where column_name <> null 90
  91. 91. where column_name is null 91
  92. 92. where column_name is null where column_name is not null 92
  93. 93. Who are the people in the database who are alive? 93
  94. 94. 94
  95. 95. 95
  96. 96. select title, year_released from movies where country = 'us' 96
  97. 97. + - databases 97
  98. 98. - databases + schemas 98
  99. 99. - databases + - schemas + tables 99
  100. 100. - databases + - schemas + - tables + columns 100
  101. 101. desc movies; 101
  102. 102. desc movies; describe table movies 102
  103. 103. desc movies; describe table movies d movies 103
  104. 104. desc movies; describe table movies d movies .schema movies 104
  105. 105. compute 105
  106. 106. compute derive 106
  107. 107. Flickr: Etsuko Nakamura 107
  108. 108. Flickr: Etsuko Nakamura 108
  109. 109. 'hello' + ' world' 109
  110. 110. 'hello' ||' world' 110
  111. 111. concat('hello' , ' world') 111
  112. 112. select title || ' was released in ' || year_released movie_release from movies where country = 'us' 112
  113. 113. select title || ' was released in ' || year_released movie_release from movies where country = 'us' 113
  114. 114. select title + ' was released in ' + cast(year_released as varchar) movie_release from movies where country = 'us' 114
  115. 115. select concat(title, ' was released in ', year_released) movie_release from movies where country = 'us' 115
  116. 116. people Age of people alive? 116
  117. 117. people born died Age of people alive? 117
  118. 118. Alive 118
  119. 119. Alive died is null 119
  120. 120. Alive died is null Age 120
  121. 121. Alive died is null Age <this year> - born 121
  122. 122. select column1, ... from some_table where some_column = some_user_input 122
  123. 123. select f(column1), ... from some_table where some_column = some_user_input 123
  124. 124. select f(column1), ... from some_table where some_column = some_user_input 124
  125. 125. select column1, ... from some_table where some_column = f(some_user_input) 125
  126. 126. select column1, ... from some_table where f(some_column) = some_user_input 126
  127. 127. case end Flickr:Tony Austin 127
  128. 128. color Y n case upper(color) when 'Y' then 'Color' when 'N' then 'B&W' else '?' end as color, ... 128
  129. 129. color Y n case upper(color) when 'Y' then 'Color' when 'N' then 'B&W' else '?' end as color, ... 129
  130. 130. color Y n Color case upper(color) when 'Y' then 'Color' when 'N' then 'B&W' else '?' end as color, ... 130
  131. 131. color Y n Color case upper(color) when 'Y' then 'Color' when 'N' then 'B&W' else '?' end as color, ... 131
  132. 132. color Y Color ? n case upper(color) when 'Y' then 'Color' when 'N' then 'B&W' else '?' end as color, ... 132
  133. 133. color Y Color ? n B&W case upper(color) when 'Y' then 'Color' when 'N' then 'B&W' else '?' end as color, ... 133
  134. 134. color Y Color ? n B&W case upper(color) when 'Y' then 'Color' when 'N' then 'B&W' else '?' end as color, ... 134
  135. 135. 135
  136. 136. case column_name when null then end 136
  137. 137. case column_name when null then end 137
  138. 138. case upper(color) when 'Y' then 'Color' when 'N' then 'B&W' else '?' end as color, 138
  139. 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
  140. 140. case when died is 'alive else 'passed end as status null then and kicking' away' 140
  141. 141. Some useful functions more in appendix B ... Flickr:Sanath Kumar 141
  142. 142. round() 142
  143. 143. round() trunc() 143
  144. 144. round(3.141592, 0) 3 trunc() 144
  145. 145. round(3.141592, 0) 3 trunc(3.141592) 3 145
  146. 146. round(3.141592, 3) 3.142 trunc(3.141592) 3 146
  147. 147. round(3.141592, 3) 3.142 trunc(3.141592, 3) 3.141 147
  148. 148. upper(), lower() 148
  149. 149. upper(), lower() substr() 149
  150. 150. upper(), lower() substr('Citizen Kane', 5, 3) 150
  151. 151. upper(), lower() substr() trim( ) 151
  152. 152. upper(), lower() substr() trim( ' Oops ') 'Oops' 152
  153. 153. upper(), lower() substr() trim( ) replace() 153
  154. 154. upper(), lower() substr() trim( ) replace('Sheep', 'ee', 'i') 'Ship' 154
  155. 155. upper(), lower() 155
  156. 156. upper(), lower() substring() 156
  157. 157. upper(), lower() substring('Citizen Kane', 5, 3) 157
  158. 158. upper(), lower() substring() ltrim() 158
  159. 159. upper(), lower() substring() ltrim(' Oops ') 'Oops ' 159
  160. 160. upper(), lower() substring() ltrim(' Oops ') 'Oops ' rtrim() 160
  161. 161. upper(), lower() substring() ltrim() rtrim() replace() 161
  162. 162. upper(), lower() substring() ltrim() rtrim() replace('Sheep', 'ee', 'i') 'Ship' 162
  163. 163. Current date 163
  164. 164. Current date Date Arithmetic FEBRUARY MARCH 164
  165. 165. Current date Date Arithmetic MARCH + 1 month + 30 days 165
  166. 166. dateadd(month, 1, date_col) 166
  167. 167. dateadd(month, 1, date_col) date_col + 1 month 167
  168. 168. dateadd(month, 1, date_col) date_col + 1 month date_col + interval'1 month' 168
  169. 169. dateadd(month, 1, date_col) date_col + 1 month date_col + interval'1 month' date_add(date_col, interval 1 month) 169
  170. 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) 170
  171. 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 171
  172. 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') 172
  173. 173. '28-DEC-1895' 173
  174. 174. '28-DEC-1895' 174
  175. 175. '28-DEC-1895' DBMS date 175
  176. 176. '12/28/1895' DBMS date 176
  177. 177. 177
  178. 178. cast( as ) 178
  179. 179. x y z 179
  180. 180. z (x + y) 180
  181. 181. z (x + y) 181
  182. 182. No duplicates 182
  183. 183. No duplicates Identifier 183
  184. 184. select country from movies 184
  185. 185. 185
  186. 186. 186
  187. 187. distinct 187
  188. 188. distinct select distinct country from movies 188
  189. 189. Aggregate functions 189
  190. 190. select country, year_released, title from movies 190
  191. 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. 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. 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
  194. 194. group by 194
  195. 195. group by select country, count(*) number_of_movies from movies group by country 195
  196. 196. group by select country country, count(*) number_of_movies from movies group by country 196
  197. 197. group by select country country, count(*) number_of_movies from movies group by country 197
  198. 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
  199. 199. select country, year_released, count(*) number_of_movies from movies group by country, year_released 199
  200. 200. select country, year_released, count(*) number_of_movies from movies group by country, year_released 200
  201. 201. select count(*) number_of_movies from movies 201
  202. 202. where 202
  203. 203. where 203
  204. 204. where 204
  205. 205. where 205
  206. 206. where 206
  207. 207. distinct, group by 207
  208. 208. distinct, group by 208
  209. 209. distinct, group by 209
  210. 210. distinct, group by 210
  211. 211. distinct, group by 211
  212. 212. count(*) count(col) min(col) max(col) avg(col) 212
  213. 213. Earliest release year by country? 213
  214. 214. Earliest release year by country? select country, min(year_released) oldest_movie from movies group by country 214
  215. 215. select country, min(year_released) oldest_movie from movies group by country 215
  216. 216. select * from ( select country, min(year_released) oldest_movie from movies group by country ) earliest_movies_per_country where oldest_movie < 1940 216
  217. 217. having select country, min(year_released) oldest_movie from movies group by country 217
  218. 218. having select country, min(year_released) oldest_movie from movies group by country having min(year_released) < 1940 218
  219. 219. SORT Flickr: Randy Robertson 219
  220. 220. select country, min(year_released) oldest_movie from movies group by country having country = 'us' 220
  221. 221. select country, min(year_released) oldest_movie from movies where country = 'us' group by country 221
  222. 222. Flickr: Dano 222
  223. 223. having Result of aggregate 223
  224. 224. Nulls? 224
  225. 225. Nulls? known + unknown = unknown 225
  226. 226. Aggregate functions ignore Nulls FLickr: Linda Åslund 226
  227. 227. select max(died) most_recent_death from people 227
  228. 228. select max(died) most_recent_death from people where died is not null 228
  229. 229. count(*) 229
  230. 230. count(*) 230
  231. 231. count(col) 231
  232. 232. count(col) 232
  233. 233. select count(*) people_count, count(born) birth_year_count, count(died) death_year_count from people 233
  234. 234. select count(colname) 234
  235. 235. select count(distinct colname) 235
  236. 236. select country, count(distinct year_released) number_of_years from movies group by country 236
  237. 237. select country, count(*) number_of_years from (select distinct country, year_released from movies) t group by country 237
  238. 238. How many people are both actors and directors? 238
  239. 239. How many people are both actors and directors? 239
  240. 240. How many people are both actors and directors? 240
  241. 241. How many people are both actors and directors? 241
  242. 242. How many people are both actors and directors? 242
  243. 243. How many people are both actors and directors? 243
  244. 244. How many people are both actors and directors? 244
  245. 245. How many people are both actors and directors? credits 245
  246. 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 246
  247. 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 247
  248. 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 248
  249. 249. 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
  250. 250. 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
  251. 251. select distinct peopleid, credited_as from credits where credited_as in ('A', 'D') 251
  252. 252. ( select distinct peopleid, credited_as from credits where credited_as in ('A', 'D')) all_actors_and_directors 252
  253. 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. 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. 255. Rows are selected with where + conditions. Beware of or. Beware of nulls. Beware of dates. 255
  256. 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. 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
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×