-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_joins_examples.sql
More file actions
229 lines (200 loc) · 6.13 KB
/
sql_joins_examples.sql
File metadata and controls
229 lines (200 loc) · 6.13 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
-- ============================================
-- SQL JOINS COMPREHENSIVE EXAMPLES
-- Portfolio Project demonstrating all join types
-- ============================================
USE company_db;
-- ============================================
-- 1. INNER JOIN
-- Returns only matching records from both tables
-- ============================================
-- Example 1.1: Get employees with their department information
SELECT
e.employee_id,
e.first_name,
e.last_name,
e.salary,
d.department_name,
d.location
FROM employees e
INNER JOIN departments d
ON e.department_id = d.department_id
ORDER BY e.employee_id;
-- Example 1.2: Get employees and their project assignments
SELECT
e.first_name,
e.last_name,
p.project_name,
ep.role,
ep.hours_worked
FROM employees e
INNER JOIN employee_projects ep
ON e.employee_id = ep.employee_id
INNER JOIN projects p
ON ep.project_id = p.project_id
ORDER BY e.last_name, p.project_name;
-- ============================================
-- 2. LEFT JOIN (LEFT OUTER JOIN)
-- Returns all records from left table + matching from right
-- ============================================
-- Example 2.1: Get all employees and their departments (including those without departments)
SELECT
e.employee_id,
e.first_name,
e.last_name,
e.salary,
d.department_name,
d.location
FROM employees e
LEFT JOIN departments d
ON e.department_id = d.department_id
ORDER BY e.employee_id;
-- Example 2.2: Get all projects and their assigned employees (including projects without employees)
SELECT
p.project_name,
p.budget,
e.first_name,
e.last_name,
ep.role
FROM projects p
LEFT JOIN employee_projects ep
ON p.project_id = ep.project_id
LEFT JOIN employees e
ON ep.employee_id = e.employee_id
ORDER BY p.project_name;
-- ============================================
-- 3. RIGHT JOIN (RIGHT OUTER JOIN)
-- Returns all records from right table + matching from left
-- ============================================
-- Example 3.1: Get all departments and their employees (including departments without employees)
SELECT
d.department_name,
d.location,
e.first_name,
e.last_name,
e.salary
FROM employees e
RIGHT JOIN departments d
ON e.department_id = d.department_id
ORDER BY d.department_name, e.last_name;
-- ============================================
-- 4. FULL OUTER JOIN
-- Returns all records when there's a match in either table
-- Note: MySQL doesn't support FULL OUTER JOIN directly
-- Use UNION of LEFT and RIGHT joins
-- ============================================
-- Example 4.1: Get all employees and departments (show unmatched from both sides)
SELECT
e.employee_id,
e.first_name,
e.last_name,
d.department_id,
d.department_name
FROM employees e
LEFT JOIN departments d
ON e.department_id = d.department_id
UNION
SELECT
e.employee_id,
e.first_name,
e.last_name,
d.department_id,
d.department_name
FROM employees e
RIGHT JOIN departments d
ON e.department_id = d.department_id;
-- ============================================
-- 5. CROSS JOIN
-- Returns Cartesian product (all possible combinations)
-- ============================================
-- Example 5.1: Get all possible employee-project combinations
-- (Useful for generating all possible assignments)
SELECT
e.first_name,
e.last_name,
p.project_name
FROM employees e
CROSS JOIN projects p
ORDER BY e.last_name, p.project_name;
-- ============================================
-- 6. SELF JOIN
-- Join a table to itself
-- ============================================
-- Example 6.1: Find employees in the same department (employee pairs)
SELECT
e1.first_name AS employee1_first,
e1.last_name AS employee1_last,
e2.first_name AS employee2_first,
e2.last_name AS employee2_last,
d.department_name
FROM employees e1
INNER JOIN employees e2
ON e1.department_id = e2.department_id
AND e1.employee_id < e2.employee_id -- Prevent duplicate pairs
INNER JOIN departments d
ON e1.department_id = d.department_id
ORDER BY d.department_name;
-- ============================================
-- ADVANCED QUERIES WITH JOINS
-- ============================================
-- Example A1: Department employee count and average salary
SELECT
d.department_name,
d.location,
COUNT(e.employee_id) AS employee_count,
ROUND(AVG(e.salary), 2) AS avg_salary
FROM departments d
LEFT JOIN employees e
ON d.department_id = e.department_id
GROUP BY d.department_id, d.department_name, d.location
ORDER BY employee_count DESC;
-- Example A2: Employees working on multiple projects
SELECT
e.first_name,
e.last_name,
COUNT(ep.project_id) AS project_count,
SUM(ep.hours_worked) AS total_hours
FROM employees e
INNER JOIN employee_projects ep
ON e.employee_id = ep.employee_id
GROUP BY e.employee_id, e.first_name, e.last_name
HAVING COUNT(ep.project_id) > 1
ORDER BY project_count DESC;
-- Example A3: Projects with budget and total hours worked
SELECT
p.project_name,
p.budget,
COUNT(DISTINCT ep.employee_id) AS team_size,
SUM(ep.hours_worked) AS total_hours,
ROUND(p.budget / NULLIF(SUM(ep.hours_worked), 0), 2) AS cost_per_hour
FROM projects p
LEFT JOIN employee_projects ep
ON p.project_id = ep.project_id
GROUP BY p.project_id, p.project_name, p.budget
ORDER BY p.project_name;
-- Example A4: Find employees not assigned to any project
SELECT
e.employee_id,
e.first_name,
e.last_name,
d.department_name
FROM employees e
LEFT JOIN employee_projects ep
ON e.employee_id = ep.employee_id
LEFT JOIN departments d
ON e.department_id = d.department_id
WHERE ep.employee_id IS NULL
ORDER BY e.last_name;
-- Example A5: Department project involvement
SELECT
d.department_name,
p.project_name,
COUNT(DISTINCT e.employee_id) AS employees_assigned
FROM departments d
INNER JOIN employees e
ON d.department_id = e.department_id
INNER JOIN employee_projects ep
ON e.employee_id = ep.employee_id
INNER JOIN projects p
ON ep.project_id = p.project_id
GROUP BY d.department_id, d.department_name, p.project_id, p.project_name
ORDER BY d.department_name, p.project_name;