-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL_querying_6_Joins.sql
More file actions
100 lines (66 loc) · 2.35 KB
/
SQL_querying_6_Joins.sql
File metadata and controls
100 lines (66 loc) · 2.35 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
#### This script is for use with the Parks_and_Rec database created with: MySQL-YouTube-Series/Beginner - Parks_and_Rec_Create_db.sql
## Joins
SELECT *
FROM parks_and_recreation.employee_demographics
;
SELECT gender, AVG(age)
FROM parks_and_recreation.employee_salary
;
# both tables contain the employee_id field
#### inner join
# rows that appear in BOTH tables
# inner join is the default if only JOIN is specified
SELECT *
FROM parks_and_recreation.employee_demographics
INNER JOIN parks_and_recreation.employee_salary
ON employee_demographics.nemployee_id = employee_salary.employee_id
;
# combine with aliasing to shorten the names of everything
SELECT *
FROM parks_and_recreation.employee_demographics AS dem
INNER JOIN parks_and_recreation.employee_salary AS sal
ON dem.employee_id = sal.employee_id
;
# if selecting cols present in > table, you must specify in the select argument
SELECT dem.employee_id, age, occupation
FROM parks_and_recreation.employee_demographics AS dem
INNER JOIN parks_and_recreation.employee_salary AS sal
ON dem.employee_id = sal.employee_id
;
## outer joins
# left takes everythign from the left (first) and everything matching in right
# right takes everything from the right (second) and everything matching from left
SELECT dem.employee_id, age, occupation
FROM parks_and_recreation.employee_demographics AS dem
LEFT JOIN parks_and_recreation.employee_salary AS sal
ON dem.employee_id = sal.employee_id
;
SELECT dem.employee_id, age, occupation
FROM parks_and_recreation.employee_demographics AS dem
RIGHT JOIN parks_and_recreation.employee_salary AS sal
ON dem.employee_id = sal.employee_id
;
## self joins
# ties a table to itself
# assign secret santa pairs
SELECT emp1.employee_id AS emp_santa,
emp1.first_name AS first_name_santa,
emp1.last_name AS first_name_santa,
emp2.employee_id AS emp_name,
emp2.first_name AS first_name_emp
emp2.last_name AS last_name_emp
FROM parks_and_recreation.employee_salary AS emp1
JOIN parks_and_recreation.employee_salary AS emp2
ON emp1.employee_id + 1 = emp2.employee_id
;
## joining multiple tables
#
SELECT *
FROM parks_departments
SELECT *
FROM parks_and_recreation.employee_demographics AS dem
INNER JOIN parks_and_recreation.employee_salary AS sal
ON dem.nemployee_id = sal.employee_id
INNER JOIN parks_departments AS pd
ON sal.dept.id = pd.department_id
;