-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay_07 - Regexp, If, Case, Conditional Statements.sql
372 lines (289 loc) · 9.11 KB
/
Day_07 - Regexp, If, Case, Conditional Statements.sql
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# Regexp (pattern matching, it is like contains filter in excel)
# ^ (Starts with)
# $ (ends with)
# | (or)
# [] (range of values)
# {} (repeat n times)
Select
Title
from movies.film
Where Title regexp 'Star' ;
Select
Title
from movies.film
Where Title regexp '^Star' ;
Select
Title
from movies.film
Where Title regexp 'Stars$' ;
Select
Title
from movies.film
Where Title regexp 'Star|King|Die' ;
Select
Title
from movies.film
Where Title regexp 'Star|King$|^Die' ;
Select
Title
from movies.film
where Title regexp 'x|y|z' ;
Select
Title
from movies.film
where Title regexp '[xyz]' ;
Select
Title
from movies.film
where Title regexp '[uvwxyz]' ;
Select
Title
from movies.film
where Title regexp '[u-z]' ;
Select
Title
from movies.film
where Title regexp 'Star' ;
Select
Title
from movies.film
where Title regexp '[Star]' ;
Select
Title
from movies.film
where Title regexp '[0-9]' ;
Select
Title
from movies.film
where Title regexp '[xyz]$' ;
Select
Title
from movies.film
where Title regexp 'r{2}' ;
# 1. Display the list of films starting with vowels (aeiou) but not ending with vowels(aeiou)
Select
Title
from movies.film
Where Title regexp '^[aeiou]' and Title not regexp '[aeiou]$' ;
# 2. Display the list of films containing only numbers
Select
Title
from movies.film
where Title not regexp '[A-Z]' ;
# 3. Display the list of Hit films with 0 repeated atleast 2 times in a word
Select
Title
from movies.film
where Title regexp '0{2}' and BoxOfficeDollars > BudgetDollars ;
# 4. Display the list of films starting with c or h but ending with od
Select
Title
from movies.film
where Title regexp '^[ch]' and Title regexp 'od$' ;
# 5. Display the list of films starting with number but not ending with number
Select
Title
from movies.film
where Title regexp '^[0-9]' and Title not regexp '[0-9]$';
# 6. Display the list of films starting with number or ending with x,y,z or containing the word King
Select
Title
from movies.film
where Title regexp '^[0-9]|[xyz]$|King' ;
# IF, Case when - Conditional Statements in SQL
Select
Title,
BoxofficeDollars as Boxoffice,
BudgetDollars as Budget,
BoxofficeDollars - BudgetDollars as PL
from movies.film ;
Alter table movies.film
add PL double after BudgetDollars ;
set sql_safe_updates = 0 ;
Update movies.film
set PL = BoxofficeDollars - BudgetDollars ;
Alter table movies.film
drop column PL ;
# Math Function
Select
Title, RunTimeMinutes,
RunTimeMinutes/60 as Hours,
Round(RunTimeMinutes/60) as rhours,
Floor(RunTimeMinutes/60) as fhours,
Ceiling(RunTimeMinutes/60) as chours,
RunTimeMinutes % 60 as Minutes
from movies.film ;
Select
Title,RunTimeMinutes,
Concat(Floor(RunTimeMinutes/60),' Hours ', RunTimeMinutes % 60,' Minutes') as Duration
from movies.film ;
Select
concat(First,' ',Last) as FullName, Status,
If(Status = 'Full Time','Permanent','Temporary') as EmpType
from employees.employees ;
Select
concat(First,' ',Last) as 'Full Name', Status, Salary,
If(Status = 'Full Time',Salary * 1.1,Salary) as NewSalary
from employees.employees ;
# Salary * 0.1 + salary
# Salary(0.1 + 1)
# Salary * 1.1
Select
Title,OscarWins,
If(OscarWins = 0,'Average Film', If(OscarWins < 5,'Great Film','Classic Film')) as MovieType
from movies.film ;
# 1. Hike the Salary by 10% for all full time employees and only those contract employees with jobrating 1
Select
concat(First,' ',Last) as FullName, Status, Jobrating, Salary,
If(Status = 'Full Time' or (Status = 'Contract' and Jobrating = 1), Salary * 1.1, Salary) as NewSalary
from employees.employees ;
# 2. Hike the Salary by 10% for only full time employees hired before 2000 Year
Select
concat(First,' ',Last) as FullName, Status, Hiredate, Salary,
If(Status = 'Full Time' and HireDate < '2000-1-1', Salary * 1.1, Salary) as NewSalary
from employees.employees ;
# 3. Display as classic blockbuster if boxofficedollars more than 1e9 and oscarwins more than zero,else others
Select
Title,BoxofficeDollars,OscarWins,
If(BoxofficeDollars > 1e9 and OscarWins >0,'Classic Blockbuster','Others') as MovieType
from movies.film ;
# 4. Display Text Title if Title contains only Text, else alphanumeric Title
Select
Title,
If(Title not regexp '[0-9]','Text Title','Alphanumeric Title') as MovieType
from movies.film ;
# 5. Hike the salary by 10% if jobrating is 4 or more,by 5% if jobrating is 2-3,else same salary
Select
concat(First,' ',Last) as FullName, Jobrating,
If(Jobrating >=4,Salary * 1.1,If(Jobrating >=2, Salary * 1.05, Salary)) as NewSalary
from employees.employees ;
# 6. Display Short Film if RunTimeMinutes less than 100, Avg Length Film if RunTimeMinutes 100-160, else Long Film
Select
Title,RunTimeMinutes,
If(RunTimeMinutes < 100,'Short Film',If(RunTimeMinutes < 160,'Avg Length film','Long film')) as MovieType
from movies.film ;
# 7. Display Text Title if Title contains only Text, alphanumeric Title if it contains text and number,else numeric Title
Select
Title,
If(Title not regexp '[0-9]','Text Title',If(Title regexp '[A-Z]','Alphanumeric Title','Numeric Title')) as MovieType
from movies.film ;
# 8. Display Classic Blockbuster if boxoffice more than 1e9 and oscarwins more than 0,Blockbuster if Boxoffice more than 1e9 without oscars, else others
Select
Title,BoxofficeDollars,OscarWins,
If(BoxofficeDollars > 1e9 and OscarWins >0,'Classic Blockbuster', If(BoxofficeDollars > 1e9,'Blockbuster','Others')) as MovieType
from movies.film ;
# 9. Display Old Actor if actor dob before 1970, middle aged actor if actor dob 1970-1990, else young actor
Select
concat(FirstName,' ',FamilyName) as FullName, Dob,
If(Dob < '1970-1-1','Old Actor',If(Dob < '1990-1-1','Middle Aged Actor','Young Actor')) as ActorType
from movies.actor ;
# Rewrite all the above queries using case statement
# Case
# when condition1 is true then Result1
# when condition2 is true then Result2
# else Result3
# end
Select
Title,
If(BoxofficeDollars > 1e9, 'Blockbuster', 'Others') as MovieType
from movies.film ;
Select
Title,
case
when BoxofficeDollars > 1e9 then 'Blockbuster'
end as MovieType
from movies.film ;
Select
concat(First,' ',Last) as FullName, Status,
case
when Status = 'Full Time' then 'Permanent'
when Status = 'Contract' then 'Temporary'
else 'Terminated'
end as EmpType
from employees.employees ;
Select
concat(First,' ',Last) as FullName, Status, Salary,
case
when Status = 'Full Time' then round(Salary * 1.1)
when Status = 'Contract' then round(Salary * 1.05)
else Salary
end as NewSalary
from employees.employees ;
# 1. Hike the Salary by 10% for all full time employees and only those contract employees with jobrating 1
Select
concat(First,' ',Last) as FullName, Status, Jobrating, Salary,
case
when Status = 'Full Time' or (Status = 'Contract' and Jobrating = 1) then round(Salary * 1.1)
else Salary
end as NewSalary
from employees.employees ;
# 2. Hike the Salary by 10% for only full time employees hired before 2000 Year
Select
concat(First,' ',Last) as FullName, Status, HireDate, Salary,
case
when Status = 'Full Time' and HireDate > 2000-01-01 then round(Salary * 1.1)
else Salary
end as NewSalary
from employees.employees ;
# 3. Display as classic blockbuster if boxofficedollars more than 1e9 and oscarwins more than zero,else others
Select
Title, BoxofficeDollars, OscarWins,
case
when BoxofficeDollars > 1e9 and OscarWins > 0 then 'Classic Blockbuster'
else 'Others'
end as MovieType
from movies.film ;
# 4. Display Text Title if Title contains only Text, else alphanumeric Title
Select
Title,
case
when Title not regexp '[0-9]' then 'Text Title'
else 'Alphanumeric Title'
end as MovieType
from movies.film ;
# 5. Hike the salary by 10% if jobrating is 4 or more, by 5% if jobrating is 2-3, else same salary
Select
concat(First,' ',Last) as FullName, Jobrating, Salary,
case
when Jobrating >=4 then round(Salary * 1.1)
when Jobrating >=2 then round(Salary * 1.05)
else Salary
end as NewSalary
from employees.employees ;
# 6. Display Short Film if RunTimeMinutes less than 100, Avg Length Film if RunTimeMinutes 100-160, else Long Film
Select
Title, RunTimeMinutes,
case
when RunTimeMinutes < 100 then 'Short Film'
when RunTimeMinutes < 160 then 'Avg Length film'
else 'Long film'
end as MovieType
from movies.film ;
# 7. Display Text Title if Title contains only Text, alphanumeric Title if it contains text and number, else numeric Title
Select
Title,
case
when Title not regexp '[0-9]' then 'Text Title'
when Title regexp '[A-Z]' then 'Alphanumeric Title'
else 'Numeric Title'
end as MovieType
from movies.film ;
# 8. Display Classic Blockbuster if boxoffice more than 1e9 and oscarwins more than 0, Blockbuster if Boxoffice more than 1e9 without oscars, else others
Select
Title, BoxofficeDollars, OscarWins,
case
when BoxofficeDollars > 1e9 and OscarWins > 0 then 'Classic Blockbuster'
when BoxofficeDollars > 1e9 then 'Blockbuster'
else 'Others'
end as MovieType
from movies.film ;
# 9. Display Old Actor if actor dob before 1970, middle aged actor if actor dob 1970-1990, else young actor
Select
concat(FirstName,' ',FamilyName) as FullName, DoB,
case
when DoB < '1970-1-1' then 'Old Actor'
when DoB < '1990-1-1' then 'Middle Aged Actor'
else 'Young Actor'
end as ActorType
from movies.actor ;
-- ======================================================== THE END ========================================================