-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathqueries.sql
More file actions
656 lines (548 loc) · 13.7 KB
/
queries.sql
File metadata and controls
656 lines (548 loc) · 13.7 KB
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
-- SELECT statement
-- select all (*) columns from table
SELECT * FROM employees;
SELECT * FROM shops;
SELECT * FROM locations;
SELECT * FROM suppliers;
-- select some (3) columns of table
SELECT
employee_id,
first_name,
last_name
FROM employees;
--===================================================
-- WHERE clause + AND & OR
-- Select only the employees who make more than 50k
SELECT *
FROM employees
WHERE salary > 50000;
-- Select only the employees who work in Common Grounds coffeshop
SELECT *
FROM employees
WHERE coffeeshop_id = 1;
-- Select all the employees who work in Common Grounds and make more than 50k
SELECT *
FROM employees
WHERE salary > 50000 AND coffeeshop_id = 1;
-- Select all the employees who work in Common Grounds or make more than 50k
SELECT *
FROM employees
WHERE salary > 50000 OR coffeeshop_id = 1;
-- Select all the employees who work in Common Grounds, make more than 50k and are male
SELECT *
FROM employees
WHERE
salary > 50000
AND coffeeshop_id = 1
AND gender = 'M';
-- Select all the employees who work in Common Grounds or make more than 50k or are male
SELECT *
FROM employees
WHERE
salary > 50000
OR coffeeshop_id = 1
OR gender = 'M';
--=======================================================
-- IN, NOT IN, IS NULL, BETWEEN
-- Select all rows from the suppliers table where the supplier is Beans and Barley
SELECT *
FROM suppliers
WHERE supplier_name = 'Beans and Barley';
-- Select all rows from the suppliers table where the supplier is NOT Beans and Barley
SELECT *
FROM suppliers
WHERE NOT supplier_name = 'Beans and Barley';
SELECT *
FROM suppliers
WHERE supplier_name <> 'Beans and Barley';
-- Select all Robusta and Arabica coffee types
SELECT *
FROM suppliers
WHERE coffee_type IN ('Robusta', 'Arabica');
SELECT *
FROM suppliers
WHERE
coffee_type = 'Robusta'
OR coffee_type = 'Arabica';
-- Select all coffee types that are not Robusta or Arabica
SELECT *
FROM suppliers
WHERE coffee_type NOT IN ('Robusta', 'Arabica');
-- Select all employees with missing email addresses
SELECT *
FROM employees
WHERE email IS NULL;
-- Select all employees whose emails are not missing
SELECT *
FROM employees
WHERE NOT email IS NULL;
-- Select all employees who make between 35k and 50k
SELECT
employee_id,
first_name,
last_name,
salary
FROM employees
WHERE salary BETWEEN 35000 AND 50000;
SELECT
employee_id,
first_name,
last_name,
salary
FROM employees
WHERE
salary >= 35000
AND salary <= 50000;
--===========================================================
-- ORDER BY, LIMIT, DISTINCT, Renaming columns
-- Order by salary ascending
SELECT
employee_id,
first_name,
last_name,
salary
FROM employees
ORDER BY salary;
-- Order by salary descending
SELECT
employee_id,
first_name,
last_name,
salary
FROM employees
ORDER BY salary DESC;
-- Top 10 highest paid employees
SELECT
employee_id,
first_name,
last_name,
salary
FROM employees
ORDER BY salary DESC
LIMIT 10;
-- Return all unique coffeeshop ids
SELECT DISTINCT coffeeshop_id
FROM employees;
-- Return all unique countries
SELECT DISTINCT country
FROM locations;
-- Renaming columns
SELECT
email,
email AS email_address,
hire_date,
hire_date AS date_joined,
salary,
salary AS pay
FROM employees;
--=========================================================
-- EXTRACT
SELECT
hire_date as date,
EXTRACT(YEAR FROM hire_date) AS year,
EXTRACT(MONTH FROM hire_date) AS month,
EXTRACT(DAY FROM hire_date) AS day
FROM employees;
--=========================================================
-- UPPER, LOWER, LENGTH, TRIM
-- Uppercase first and last names
SELECT
first_name,
UPPER(first_name) AS first_name_upper,
last_name,
UPPER(last_name) AS last_name_upper
FROM employees;
-- Lowercase first and last names
SELECT
first_name,
LOWER(first_name) AS first_name_upper,
last_name,
LOWER(last_name) AS last_name_upper
FROM employees;
-- Return the email and the length of emails
SELECT
email,
LENGTH(email) AS email_length
FROM employees;
-- TRIM
SELECT
LENGTH(' HELLO ') AS hello_with_spaces,
LENGTH('HELLO') AS hello_no_spaces,
LENGTH(TRIM(' HELLO ')) AS hello_trimmed;
--=========================================================
-- Concatenation, Boolean expressions, wildcards
-- Concatenate first and last names to create full names
SELECT
first_name,
last_name,
first_name || ' ' || last_name AS full_name
FROM employees;
-- Concatenate columns to create a sentence
SELECT
first_name || ' ' || last_name || ' makes ' || salary
FROM employees;
-- Boolean expressios
-- if the person makes less than 50k, then true, otherwise false
SELECT
first_name || ' ' || last_name AS full_name,
(salary < 50000) AS less_than_50k
FROM employees;
-- if the person is a female and makes less than 50k, then true, otherwise false
SELECT
first_name || ' ' || last_name AS full_name,
(salary < 50000 AND gender = 'F') AS less_than_50k_female
FROM employees;
-- Boolean expressions with wildcards (% subString)
-- if email has '.com', return true, otherwise false
SELECT
email,
(email like '%.com%') AS dotcom_flag
FROM employees;
SELECT
email,
(email like '%.gov%') AS dotgov_flag
FROM employees;
-- return only government employees
select
first_name || ' ' || last_name as full_name,
email as gov_email
from employees
where email like '%.gov%';
--==========================================================
-- SUBSTRING, POSITION, COALESCE
-- SUBSTRING
-- Get the email from the 5th character
SELECT
email,
SUBSTRING(email FROM 5)
FROM employees;
-- POSITION
-- Find the position of '@' in the email
SELECT
email,
POSITION('@' IN email)
FROM employees;
-- SUBSTRING & POSITION to find the email client
SELECT
email,
SUBSTRING(email FROM POSITION('@' IN email))
FROM employees;
SELECT
email,
SUBSTRING(email FROM POSITION('@' IN email) + 1)
FROM employees;
-- COALESCE to fill missing emails with custom value
SELECT
email,
COALESCE(email, 'NO EMAIL PROVIDED')
FROM employees;
--===================================================
-- MIN, MAX, AVG, SUM, COUNT
-- Select the minimum salary
SELECT MIN(salary) as min_sal
FROM employees;
-- Select the maximum salary
SELECT MAX(salary) as max_sal
FROM employees;
-- Select difference between maximum and minimum salary
SELECT MAX(salary) - MIN(salary)
FROM employees;
-- Select the average salary
SELECT AVG(salary)
FROM employees;
-- Round average salary to nearest integer
SELECT ROUND(AVG(salary),0)
FROM employees;
-- Sum up the salaries
SELECT SUM(salary)
FROM employees;
-- Count the number of entries
SELECT COUNT(*)
FROM employees;
SELECT COUNT(salary)
FROM employees;
SELECT COUNT(email)
FROM employees;
-- summary
SELECT
MIN(salary) as min_sal,
MAX(salary) as max_sal,
MAX(salary) - MIN(salary) as diff_sal,
round(avg(salary), 0) as average_sal,
sum(salary) as total_sal,
count(*) as num_of_emp
FROM employees;
--=========================================================
-- GROUP BY & HAVING
-- Return the number of employees for each coffeeshop
SELECT
coffeeshop_id,
COUNT(employee_id)
FROM employees
GROUP BY coffeeshop_id;
-- Return the total salaries for each coffeeshop
SELECT
coffeeshop_id,
SUM(salary)
FROM employees
GROUP BY coffeeshop_id;
-- Return the number of employees, the avg & min & max & total salaries for each coffeeshop
SELECT
coffeeshop_id,
COUNT(*) AS num_of_emp,
ROUND(AVG(salary), 0) AS avg_sal,
MIN(salary) AS min_sal,
MAX(salary) AS max_sal,
SUM(salary) AS total_sal
FROM employees
GROUP BY coffeeshop_id
ORDER BY num_of_emp DESC;
-- HAVING
-- After GROUP BY, return only the coffeeshops with more than 200 employees
SELECT
coffeeshop_id,
COUNT(*) AS num_of_emp,
ROUND(AVG(salary), 0) AS avg_sal,
MIN(salary) AS min_sal,
MAX(salary) AS max_sal,
SUM(salary) AS total_sal
FROM employees
GROUP BY coffeeshop_id
HAVING COUNT(*) > 200 -- filter, alter "where" after "gruop by"
ORDER BY num_of_emp DESC;
-- After GROUP BY, return only the coffeeshops with a minimum salary of less than 10k
SELECT
coffeeshop_id,
COUNT(*) AS num_of_emp,
ROUND(AVG(salary), 0) AS avg_sal,
MIN(salary) AS min_sal,
MAX(salary) AS max_sal,
SUM(salary) AS total_sal
FROM employees
GROUP BY coffeeshop_id
HAVING MIN(salary) < 10000
ORDER BY num_of_emp DESC;
--===========================================================
-- CASE, CASE with GROUP BY, and CASE for transposing data
-- CASE
-- If pay is less than 50k, then low pay, otherwise high pay
SELECT
employee_id,
first_name || ' ' || last_name as full_name,
salary,
CASE
WHEN salary < 50000 THEN 'low pay'
WHEN salary >= 50000 THEN 'high pay'
ELSE 'no pay'
END as pay_category
FROM employees
ORDER BY salary DESC;
-- If pay is less than 20k, then low pay
-- if between 20k-50k inclusive, then medium pay
-- if over 50k, then high pay
SELECT
employee_id,
first_name || ' ' || last_name as full_name,
salary,
CASE
WHEN salary < 20000 THEN 'low pay'
WHEN salary BETWEEN 20000 and 50000 THEN 'medium pay'
WHEN salary > 50000 THEN 'high pay'
ELSE 'no pay'
END as pay_category
FROM employees
ORDER BY salary DESC;
-- CASE & GROUP BY
-- Return the count of employees in each pay category
SELECT a.pay_category, COUNT(*)
FROM(
SELECT
employee_id,
first_name || ' ' || last_name as full_name,
salary,
CASE
WHEN salary < 20000 THEN 'low pay'
WHEN salary BETWEEN 20000 and 50000 THEN 'medium pay'
WHEN salary > 50000 THEN 'high pay'
ELSE 'no pay'
END as pay_category
FROM employees
ORDER BY salary DESC
) a
GROUP BY a.pay_category;
-- Transpose above
SELECT
SUM(CASE WHEN salary < 20000 THEN 1 ELSE 0 END) AS low_pay,
SUM(CASE WHEN salary BETWEEN 20000 AND 50000 THEN 1 ELSE 0 END) AS medium_pay,
SUM(CASE WHEN salary > 50000 THEN 1 ELSE 0 END) AS high_pay
FROM employees;
--================================================
-- JOIN
-- Inserting values just for JOIN exercises
INSERT INTO locations VALUES (4, 'Paris', 'France');
INSERT INTO shops VALUES (6, 'Happy Brew', NULL);
-- Checking the values we inserted
SELECT * FROM shops;
SELECT * FROM locations;
-- "INNER JOIN" same as just "J0iN"
SELECT
s.coffeeshop_name,
l.city,
l.country
FROM (
shops s
inner JOIN locations as l
ON s.city_id = l.city_id
);
SELECT
s.coffeeshop_name,
l.city,
l.country
FROM
shops s
JOIN locations l
ON s.city_id = l.city_id;
-- LEFT JOIN
SELECT
s.coffeeshop_name,
l.city,
l.country
FROM
shops s
LEFT JOIN locations l
ON s.city_id = l.city_id;
-- RIGHT JOIN
SELECT
s.coffeeshop_name,
l.city,
l.country
FROM
shops s
RIGHT JOIN locations l
ON s.city_id = l.city_id;
-- FULL OUTER JOIN
SELECT
s.coffeeshop_name,
l.city,
l.country
FROM
shops s
FULL OUTER JOIN locations l
ON s.city_id = l.city_id;
-- Delete the values we created just for the JOIN exercises
DELETE FROM locations WHERE city_id = 4;
DELETE FROM shops WHERE coffeeshop_id = 6;
--========================================================
-- UNION (to stack data on top each other)
-- Return all cities and countries
SELECT city FROM locations
UNION
SELECT country FROM locations;
-- UNION removes duplicates
SELECT country FROM locations
UNION
SELECT country FROM locations;
-- UNION ALL keeps duplicates
SELECT country FROM locations
UNION ALL
SELECT country FROM locations;
-- Return all coffeeshop names, cities and countries
SELECT coffeeshop_name FROM shops
UNION
SELECT city FROM locations
UNION
SELECT country FROM locations;
--=================================================
-- Subqueries
-- Basic subqueries with subqueries in the FROM clause
SELECT *
FROM (
SELECT *
FROM employees
where coffeeshop_id IN (3,4)
) as a;
SELECT
a.employee_id,
a.first_name,
a.last_name
FROM (
SELECT *
FROM employees
where coffeeshop_id IN (3,4)
) a;
-- Basic subqueries with subqueries in the SELECT clause
SELECT
first_name,
last_name,
salary,
(
SELECT MAX(salary)
FROM employees
LIMIT 1
) max_sal
FROM employees;
SELECT
first_name,
last_name,
salary,
(
SELECT ROUND(AVG(salary), 0)
FROM employees
LIMIT 1
) avg_sal
FROM employees;
SELECT
first_name,
last_name,
salary,
salary - ( -- avg_sal
SELECT ROUND(AVG(salary), 0)
FROM employees
LIMIT 1
) avg_sal_diff
FROM employees;
-- Subqueries in the WHERE clause
-- Return all US coffee shops
SELECT *
FROM shops
WHERE city_id IN ( -- US city_id's
SELECT city_id
FROM locations
WHERE country = 'United States'
);
-- Return all employees who work in US coffee shops
SELECT *
FROM employees
WHERE coffeeshop_id IN ( -- US coffeeshop_id's
SELECT coffeeshop_id
FROM shops
WHERE city_id IN ( -- US city-id's
SELECT city_id
FROM locations
WHERE country = 'United States'
)
);
-- Return all employees who make over 35k and work in US coffee shops
SELECT *
FROM employees
WHERE salary > 35000 AND coffeeshop_id IN ( -- US coffeeshop_id's
SELECT coffeeshop_id
FROM shops
WHERE city_id IN ( -- US city_id's
SELECT city_id
FROM locations
WHERE country = 'United States'
)
);
-- 30 day moving total pay
-- The inner query calculates the total_salary of employees who were hired "within" the 30-day period before the hire_date of the current employee
SELECT
hire_date,
salary,
(
SELECT SUM(salary)
FROM employees e2
WHERE e2.hire_date BETWEEN e1.hire_date - 30 AND e1.hire_date
) AS pay_pattern
FROM employees e1
ORDER BY hire_date;