-
Notifications
You must be signed in to change notification settings - Fork 525
/
Copy path05_SQL_Deep_Dive.sql
538 lines (408 loc) · 13.1 KB
/
05_SQL_Deep_Dive.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/************ 2) Simple Exercises *********/
/* List all emmployees */
SELECT * FROM employees LIMIT 10;
/* How many departments? */
SELECT COUNT(*) FROM departments;
/* How many times has Employee 10001 has a raise? */
SELECT COUNT(*) AS Number_of_raises FROM salaries
WHERE emp_no = 10001;
/* What title has 10006 has? */
SELECT title FROM titles
WHERE emp_no = 10006;
/***************** 5) Column Concat *****************/
SELECT CONCAT(emp_no, ' is a ', title) AS "EmpTitle" FROM titles
LIMIT 5;
SELECT emp_no,
CONCAT(first_name, ' ', last_name) AS "Full Name"
FROM employees
LIMIT 5;
/************** 6) Types of Functions in SQL *************/
/*
Aggerate - operate on MANY records to produce ONE value, example: SUM of salaries
Scalar - operate on EACH record Independently, example: CONCAT , it doesn't return result of concat values as one value.
*/
/*********** 7) Aggregate Functions ************/
/*
AVG()
COUNT()
MIN()
MAX()
SUM()
*/
SELECT COUNT(*) FROM employees;
SELECT MIN(emp_no) FROM employees;
/* Get the highest salary avaliable */
SELECT MAX(salary) AS Max_Salary FROM salaries;
/* Get the total amount of salaries paid */
SELECT SUM(salary) AS Total_Salary_Paid FROM salaries;
/********** 9) Commenting your queries *******/
SELECT * FROM employees
WHERE first_name='Mayumi' AND last_name='Schueller';
/*********** 11) Filtering Data ***********/
/* Get the list of all female employees */
SELECT * FROM employees
WHERE gender = 'F'
LIMIT 10;
/********** 12) AND OR *************/
SELECT *
FROM employees
WHERE (first_name = 'Georgi' AND last_name='Facello' AND hire_date='1986-06-26')
OR (first_name='Bezalel' AND last_name='Simmel');
/* 13) How many female customers do we have from state of Oregon or New York? */
SELECT COUNT(*) AS "NumberOfFemaleCustomers"
FROM customers
WHERE gender='F' AND (state LIKE 'OR' OR state LIKE 'NY');
/******** 15) NOT ***********/
/* How many customers aren't 55? */
SELECT COUNT(age) FROM customers
WHERE NOT age=55;
/***** 16) Comparison Operators *******/
/* 17) Exercises */
/* How many female customers do we have from the state of Oregon (OR)? */
SELECT COUNT(*)
FROM customers
WHERE gender='F' AND state='OR';
/* Who over the age of 44 has an income of 100 000 or more? */
SELECT *
FROM customers
WHERE age > 44 AND income=100000;
/* Who between the ages of 30 and 50 has an income of less than 50 000? */
SELECT *
FROM customers
WHERE age BETWEEN 30 AND 50
AND income < 50000;
/* What is the average income between the ages of 20 and 50? */
SELECT AVG(income)
FROM customers
WHERE age BETWEEN 20 AND 50;
/***** 18) Logical Operators AND OR NOT *****/
/*
Order of Operations
FROM => WEHRE => SELECT
*/
/***** 19) Operator Precedence ******/
/*
(most importance to least importance)
Parentheses
Multiplication / Division
Subtraction / Addition
NOT
AND
OR
If operators have equal precedence, then the operators are evaluated directionally.
From Left to Right or Right to Left.
check Operators Precedence.png
*/
SELECT state, gender FORM customers
WHERE gender ='F' AND (state='NY' OR state='OR');
/****** 20) Operator Precedence 2 ********/
SELECT *
FROM customers
WHERE (
income > 10000 AND state='NY'
OR (
(age > 20 AND age < 30)
AND income <=20000
)
) AND gender='F';
/*Select people either under 30 or over 50 with an income above 50000 that are from either Japan or Australia*/
SELECT *
FROM customers
WHERE (age < 30 OR age > 50)
AND income > 50000
AND (country = 'Japan' OR country = 'Australia');
/*What was our total sales in June of 2004 for orders over 100 dollars?*/
SELECT SUM(totalamount)
FROM orders
WHERE totalamount > 100
AND orderdate='2004/06';
/***** 22) Checking for NULL Values ****/
/*
NULL = NULL (output: NULL)
NULL != NULL (output: NULL)
No Matter what you do with NULL, it will always be NULL (subtract, add, equal, etc)
*/
SELECT NULL=NULL;
SELECT NULL <> NULL;
-- returns true
SELECT 1=1;
/******** 23) IS Keyword *********/
SELECT * FROM departments
WHERE dept_name = '' IS FALSE; -- meaning FALSE, but not a good way to write it.
SELECT * FROM departments
WHERE dept_name = '' IS NOT FALSE; -- meaning TRUE, but not a good way to write it.
SELECT * FROM salaries
WHERE salary < 150000 IS FALSE; -- basically saying > 150000
/******* 24) NULL Value Substituion NULL Coalesce *****/
/*
SELECT coalesce(<column>, 'Empty') AS column_alias
FROM <table>
SELECT coalesce(
<column1>,
<column2>,
<column3>,
'Empty') AS combined_columns
FROM <table>
Coalsece returns first NON NULL value.
*/
-- default age at 20, if there is no age avaliable
SELECT SUM(COALESCE(age, 20))
FROM students;
/*Assuming a student's minimum age for the class is 15, what is the average age of a student?*/
SELECT AVG(COALESCE(age, 15)) AS avg_age
FROM students;
/*Replace all empty first or last names with a default*/
SELECT COALESCE(first_name, 'DEFAULT') AS first_name,
COALESCE(last_name, 'DEFAULT') AS last_name
FROM students;
/******* 28) BETWEEN AND ********/
/*
SELECT <column>
FROM <table>
WHERE <column> BETWEEN X AND Y
*/
/******* 29) IN Keyword *******/
/*
SELECT *
FROM <table>
WHERE <column> IN (value1, vlaue2, ...)
*/
SELECT *
FROM employees
WHERE emp_no IN (100001, 100006, 11008);
/******* 31) LIKE ***********/
SELECT first_name
FROM employees
WHERE first_name LIKE 'M%';
/*
PATTERN MATCHING
LIKE '%2' : Fields that end with 2
LIKE '%2%' : Fields that have 2 anywhere in the value
LIKE '_00%' : Fields that have 2 zero's as the second and third character and anything after
LIKE '%200%' : Fields that have 200 anywhere in the value
LIKE '2_%_%' : Fields any values that start with 2 and are at least 3 characters in length
LIKE '2___3' : Find any values in a five-digit number that start with 2 and end with 3
*/
/****** CAST *******/
/* Postgres LIKE ONLY does text comparison so we must CAST whatever we use to TEXT */
CAST (salary AS text); -- method 1
salary::text -- method 2
/******** Case Insensitive Matching ILIKE *********/
name ILIKE 'BR%'; -- matching for br, BR, Br, bR
SELECT * FROM employees
WHERE first_name ILIKE 'G%GER';
SELECT * FROM employees
WHERE first_name LIKE 'g%'; -- returns nothing because casesensitive
SELECT * FROM employees
WHERE first_name ILIKE 'g%';
/*********** 33) Dates and Timezones **********/
/* Set current postgres's session to UTC */
SHOW TIMEZONE;
SET TIME ZONE UTC;
/********* 34) Setting up Timezones ***********/
ALTER USER postgres SET TIMEZONE='UTC';
/******** 35) How do we format Date and Time *********/
/*
Postgres uses IS0-8601
YYYY-MM-DDTHH:MM:SS
2021-05-04T11:01:45+08:00
*/
/******* 36) Timestamps ***********/
/* A timestamp is a date with time and timezone info */
SELECT NOW(); -- 2021-05-04 11:05:37.567804+08
CREATE TABLE timezones_tmp(
ts TIMESTAMP WITHOUT TIME ZONE,
tz TIMESTAMP WITH TIME ZONE
);
INSERT INTO timezones_tmp
VALUES(
TIMESTAMP WITHOUT TIME ZONE '2000-01-01 10:00:00',
TIMESTAMP WITH TIME ZONE '2000-01-01 10:00:00+00'
);
SELECT * FROM timezones_tmp;
/********* 37) Date Functions **********/
SELECT NOW(); /* 2021-05-04 11:17:30.418998+08 */
SELECT NOW()::date; /* 2021-05-04 */
SELECT NOW()::time; /* 11:18:22.483492 */
SELECT CURRENT_DATE; /* 2021-05-04 */
SELECT CURRENT_TIME; /* 11:19:11.726931+08:00 */
/********* Format Modifier *********/
/*
D : Day
M : Month
Y : Year
Check postgres doc for full details.
*/
SELECT TO_CHAR(CURRENT_DATE, 'dd/mm/yyyy'); /* 04/05/2021 */
SELECT TO_CHAR(CURRENT_DATE, 'ddd'); /* 124 */
/*********** 38) Date Difference and Casting ***********/
SELECT NOW() - '1800/01/01'; /* 80842 days 10:11:08.965674 */
SELECT DATE '1800/01/01'; /* 1800-01-01 */
SELECT AGE(DATE '1800/01/01'); /* 221 years 4 mons 3 days */
SELECT AGE(DATE '1990/06/30');
/* difference between two dates */
SELECT AGE(DATE '1992/11/13', DATE '1800/01/01'); /* 192 years 10 mons 12 days */
/*********** 40) Extracting Information ************/
SELECT EXTRACT (DAY FROM DATE '1992/11/13') AS DAY; /* 13 */
SELECT EXTRACT (MONTH FROM DATE '1992/11/13') AS MONTH;
SELECT EXTRACT (YEAR FROM DATE '1992/11/13') AS YEAR;
/******** ROUND A DATE (Date_Trunc) **********/
SELECT DATE_TRUNC('year', DATE '1992/11/13'); /* 1992-01-01 00:00:00+08 */
SELECT DATE_TRUNC('month', DATE '1992/11/13'); /* 1992-11-01 00:00:00+08 */
SELECT DATE_TRUNC('day', DATE '1992/11/13'); /* 1992-11-13 00:00:00+08 */
/********* 41) Intervals *********/
/* It can store and manipulate a period of time in years, months, days, hours, minutes, seconds, etc */
INTERVAL '1 year 2 months 3 days'
INTERVAL '2 weeks ago'
INTERVAL '1 year 3 hours 20 minutes'
/* <30 days before the given date */
SELECT * FROM orders
WHERE purchaseDate <= NOW() - INTERVAL '30 days';
SELECT NOW() - INTERVAL '30 days'; /* 2021-04-04 11:46:09.447478+08 */
SELECT NOW() - INTERVAL '1 year 2 months 3 days';
SELECT NOW() + INTERVAL '1 year';
/* Extracting intervals data */
-- returns 6 because nearly 6 years
SELECT EXTRACT (
YEAR
FROM INTERVAL '5 years 20 months'
);
-- returns 8, because nearest rounded months is 8 (20 months % 12)
SELECT EXTRACT (
MONTH
FROM INTERVAL '5 years 20 months'
);
/********** 43) DISTINCT *************/
/*
remove duplicates
SELECT
DISTINCT <col1>, <col2>
FROM <table>
*/
SELECT DISTINCT salary, from_date
FROM salaries
ORDER BY salary DESC;
/********* 45) Sorting Data ********/
/*
SELECT * FROM Customers
ORDER BY <column> [ASC/DESC]
*/
SELECT first_name, last_name
FROM employees
ORDER BY first_name, last_name DESC
LIMIT 10;
/******** Using Expressions *******/
SELECT DISTINCT last_name, LENGTH(last_name)
FROM employees
ORDER BY LENGTH(last_name) DESC;
/********* 46) Multi Tables SELECT *********/
SELECT e.emp_no,
CONCAT(e.first_name, e.last_name) AS full_name,
s.salary,
s.from_date, s.to_date
FROM employees as e, salaries as s
WHERE e.emp_no = s.emp_no
ORDER BY e.emp_no;
/******** 47) Inner JOIN ************/
SELECT e.emp_no,
CONCAT(e.first_name, e.last_name) AS full_name,
s.salary,
s.from_date, s.to_date
FROM employees as e
JOIN salaries as s ON s.emp_no = e.emp_no
ORDER BY e.emp_no;
/*
we want to know the latest salary after title change of the employee
salary raise happen only after 2 days of title change
*/
SELECT e.emp_no,
CONCAT(e.first_name, e.last_name) AS "Name",
s.salary,
t.title,
t.from_date AS "Promoted on"
FROM employees e
JOIN salaries s ON e.emp_no = s.emp_no
JOIN titles t ON e.emp_no = t.emp_no
AND t.from_date = (s.from_date + INTERVAL '2 days')
ORDER BY e.emp_no ASC, s.from_date ASC;
/* we want to know the original salary and also the salary at a promotion */
SELECT e.emp_no,
CONCAT(e.first_name, e.last_name) AS "Name",
s.salary,
COALESCE(t.title, 'no title change'),
COALESCE(t.from_date::text, '-') AS "title take on"
FROM employees e
JOIN salaries s ON e.emp_no = s.emp_no
JOIN titles t ON e.emp_no = t.emp_no
AND (
s.from_date = t.from_date -- original salary
OR t.from_date = (s.from_date + INTERVAL '2 days') -- promoted salary
)
ORDER BY e.emp_no ASC, s.from_date ASC;
/********* 48) Self Join *********/
/*
This usually can be done when a table has a foreign key referencing to its primary key
| id | name | startDate | supervisorId|
| 1 | David| 1990/01/01| 2 |
| 2 | Ric | 1980/06/03| |
*/
/* we want to see employee info with supervisor name */
SELECT a.id, a.name AS employee_name, a.startDate,
b.name AS supervisor_name
FROM employees a
JOIN employees b ON a.supervisorId = b.id;
/********* 49) OUTER JOIN **********/
/* Which employees are manager ? */
SELECT emp.emp_no, dept.emp_no
FROM employees emp
LEFT JOIN dept_manager dept ON emp.emp_no = dept.emp_no
WHERE dept.emp_no IS NOT NULL;
/* How many employees are NOT manager? */
SELECT COUNT(emp.emp_no)
FROM employees emp
LEFT JOIN dept_manager dept ON emp.emp_no = dept.emp_no
WHERE dept.emp_no IS NULL;
/* We want to know every salary raise and also know which ones were a promotion */
SELECT e.emp_no, s.salary,
COALESCE(t.title, 'No Title Change'),
COALESCE(t.from_date::text, '-') AS "Title taken on"
FROM employees e
JOIN salaries s ON e.emp_no = s.emp_no
LEFT JOIN titles t ON e.emp_no = t.emp_no
AND (
t.from_date = s.from_date
OR t.from_date = s.from_date + INTERVAL '2 days'
)
ORDER BY e.emp_no, s.from_date;
/******** 50) Less Common Joins ************/
/***** Cross Join ******/
/*
Create a combination of every row
*/
CREATE TABLE "cartesianA" (id INT);
CREATE TABLE "cartesianB" (id INT);
INSERT INTO "cartesianA" VALUES (1);
INSERT INTO "cartesianA" VALUES (2);
INSERT INTO "cartesianA" VALUES (3);
INSERT INTO "cartesianB" VALUES (1);
INSERT INTO "cartesianB" VALUES (2);
INSERT INTO "cartesianB" VALUES (4);
INSERT INTO "cartesianB" VALUES (5);
INSERT INTO "cartesianB" VALUES (20);
INSERT INTO "cartesianB" VALUES (30);
SELECT *
FROM "cartesianA"
CROSS JOIN "cartesianB";
/******** Full Outer Join ********/
/*
Return results from Both whether they match or not
*/
SELECT *
FROM "cartesianA" a
FULL JOIN "cartesianB" b ON a.id = b.id;
/*********** 52) USING Keyword ************/
/* Simplying the JOIN syntax */
SELECT emp.emp_no, emp.first_name, emp.last_name, d.dept_name
FROM employees emp
JOIN dept_emp dept USING(emp_no) -- same as ON emp.emp_no = dept.emp_no
JOIN departments d USING(dept_no)