-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL_querying_7_Unions.sql
More file actions
47 lines (33 loc) · 1.05 KB
/
SQL_querying_7_Unions.sql
File metadata and controls
47 lines (33 loc) · 1.05 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
#### This script is for use with the Parks_and_Rec database created with: MySQL-YouTube-Series/Beginner - Parks_and_Rec_Create_db.sql
## Unions
SELECT *
FROM employee_demographics;
# the default union is actually union distinct
SELECT first_name, last_name
FROM employee_demographics
UNION
SELECT first_name, last_name
FROM employee_salary
;
# but you can override it with union all
SELECT first_name, last_name
FROM employee_demographics
UNION ALL
SELECT first_name, last_name
FROM employee_salary
;
# for immoral budget reasons, we want to find old OR highly paid employees
# you can use multiple selects and unions to add cols where certain conditions apply - similar to an R mutate (case_when())
SELECT first_name, last_name, 'Old man' AS Label
FROM employee_demographics
WHERE age > 40 AND gender = 'Male'
UNION
SELECT first_name, last_name, 'Old lady' AS Label
FROM employee_demographics
WHERE age > 40 AND gender = 'Female'
UNION
SELECT first_name, last_name, 'Highly paid' AS Label
FROM employee_salary
WHERE salary >70000
ORDER BY first_name, last_name
;