-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNextGen SQL queries.sql
More file actions
394 lines (205 loc) · 8.87 KB
/
NextGen SQL queries.sql
File metadata and controls
394 lines (205 loc) · 8.87 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
--- All the tables---
select * from attendance
select * from department
select * from employee
select * from performance
select * from salary
select * from turnover
------EMPLOYEE------
--1 Who are the top 5 highest serving employees?
SELECT e.employee_id, e.first_name, e.last_name, e.job_title, e.hire_date, t.turnover_date, AGE(CURRENT_DATE, e.hire_date) AS years_of_service
FROM employee e
LEFT JOIN turnover t ON e.employee_id = t.employee_id
WHERE t.employee_id IS NULL -- excludes employees who left
ORDER BY e.hire_date ASC
LIMIT 5;
-- SELECT employee_id, first_name, last_name, job_title, hire_date, AGE(CURRENT_DATE, hire_date) AS years_of_service
FROM employee
ORDER BY hire_date ASC
LIMIT 5;
-- Q2 What is the turnover rate for each department?
SELECT
d.department_name,
ROUND(
(COUNT(t.turnover_id)::decimal / NULLIF(COUNT(e.employee_id), 0)) * 100,
2
) AS turnover_rate_percent
FROM department d
LEFT JOIN employee e ON d.department_id = e.department_id
LEFT JOIN turnover t ON e.employee_id = t.employee_id
GROUP BY d.department_name
ORDER BY turnover_rate_percent DESC;
-- SELECT d.department_name,
EXTRACT(YEAR FROM t.turnover_date) AS year,
COUNT(t.turnover_id) AS employees_left,
COUNT(e.employee_id) AS total_employees,
(COUNT(t.turnover_id)::decimal / NULLIF(COUNT(e.employee_id),0)) * 100 AS turnover_rate_percent
FROM department d
LEFT JOIN employee e ON d.department_id = e.department_id
LEFT JOIN turnover t ON e.employee_id = t.employee_id
GROUP BY d.department_name, EXTRACT(YEAR FROM t.turnover_date)
ORDER BY year, turnover_rate_percent DESC;
---Q3 Which employees are at risk of leaving based on their performance?
SELECT e.employee_id, e.first_name, e.last_name, p.performance_score, p.performance_date, d.department_name
FROM employee e
JOIN performance p ON e.employee_id = p.employee_id
JOIN department d ON e.department_id = d.department_id
LEFT JOIN turnover t ON e.employee_id = t.employee_id
WHERE t.turnover_id IS NOT NULL
AND p.performance_score < 4 -- threshold for underperformance
ORDER BY p.performance_score ASC
LIMIT 5;
-- SELECT e.employee_id, e.first_name, e.last_name,
ROUND(AVG(p.performance_score), 2) AS avg_performance,
CASE
WHEN AVG(p.performance_score) < 60 THEN 'High Risk'
WHEN AVG(p.performance_score) BETWEEN 60 AND 75 THEN 'Medium Risk'
ELSE 'Low Risk'
END AS risk_level
FROM employee e
JOIN performance p ON e.employee_id = p.employee_id
LEFT JOIN turnover t ON e.employee_id = t.employee_id
WHERE t.turnover_id IS NULL -- only active employees
GROUP BY e.employee_id, e.first_name, e.last_name
ORDER BY avg_performance DESC
LIMIT 5;
-- SELECT e.employee_id, e.first_name, e.last_name, p.performance_score, p.performance_date, d.department_name
FROM employee e
JOIN performance p ON e.employee_id = p.employee_id
JOIN department d ON e.department_id = d.department_id
LEFT JOIN turnover t ON e.employee_id = t.employee_id
WHERE t.turnover_id IS NULL -- still active
AND p.performance_score < 60 -- threshold based on Step 1
ORDER BY p.performance_score ASC;
-- SELECT e.employee_id, e.first_name, e.last_name,
AVG(p.performance_score) AS avg_performance,
CASE
WHEN AVG(p.performance_score) < 60 THEN 'High Risk'
WHEN AVG(p.performance_score) BETWEEN 60 AND 75 THEN 'Medium Risk'
ELSE 'Low Risk'
END AS risk_level
FROM employee e
JOIN performance p ON e.employee_id = p.employee_id
LEFT JOIN turnover t ON e.employee_id = t.employee_id
WHERE t.turnover_id IS NULL -- only active employees
GROUP BY e.employee_id, e.first_name, e.last_name;
--4 What are the main reasons employees are leaving the company?
SELECT reason_for_leaving,
COUNT(*) AS total_leavers
FROM turnover
GROUP BY reason_for_leaving
ORDER BY total_leavers DESC;
-- SELECT d.department_name, t.reason_for_leaving,
COUNT(*) AS total_leavers
FROM turnover t
JOIN employee e ON t.employee_id = e.employee_id
JOIN department d ON e.department_id = d.department_id
GROUP BY d.department_name, t.reason_for_leaving
ORDER BY d.department_name, total_leavers DESC;
-- SELECT d.department_name,
COUNT(t.turnover_id) AS total_leavers
FROM turnover t
JOIN employee e ON t.employee_id = e.employee_id
JOIN department d ON e.department_id = d.department_id
GROUP BY d.department_name
ORDER BY total_leavers DESC;
-- SELECT reason_for_leaving, d.department_name,
COUNT(*) AS total_leavers
FROM turnover t
JOIN department d ON t.department_id = d.department_id
GROUP BY d.department_name, t.reason_for_leaving
ORDER BY total_leavers DESC;
-- SELECT
d.department_name,
t.reason_for_leaving,
COUNT(*) AS total_leavers
FROM turnover t
JOIN department d ON t.department_id = d.department_id
GROUP BY d.department_name, t.reason_for_leaving
ORDER BY d.department_name, total_leavers DESC;
------PERFORMANCE------
--Q1 How many employees has left the company?
Select Count(turnover_id) As "Turnover"
From turnover
WHERE turnover_date IS NOT NULL;
-- SELECT COUNT(*) AS total_employees_left
FROM turnover;
--Q2 How many employees have a performance score of 5.0 / below 3.5?
SELECT
COUNT(DISTINCT CASE WHEN performance_score = 5.0 THEN employee_id END) AS perfect_score_employees,
COUNT(DISTINCT CASE WHEN performance_score < 3.5 THEN employee_id END) AS low_score_employees
FROM performance;
--SELECT
COUNT(*) FILTER (WHERE performance_score = 5.0) AS employees_with_5_score,
COUNT(*) FILTER (WHERE performance_score < 3.5) AS employees_below_3_5
FROM (
SELECT
employee_id, performance_score
FROM performance
ORDER BY employee_id, performance_date DESC
) AS Review;
-- SELECT
SUM(CASE WHEN performance_score = 5.0 THEN 1 ELSE 0 END) AS perfect_score_employees,
SUM(CASE WHEN performance_score < 3.5 THEN 1 ELSE 0 END) AS low_score_employees
FROM performance;
SELECT
COUNT(DISTINCT CASE WHEN performance_score = 5.0 THEN employee_id END) AS perfect_score_employees,
COUNT(DISTINCT CASE WHEN performance_score < 3.5 THEN employee_id END) AS low_score_employees
FROM performance;
--Q3 Which department has the most employees with a performance of 5.0 / below 3.5?
SELECT d.department_name,
COUNT(CASE WHEN p.performance_score = 5.0 THEN 1 END) AS score_5_0_count,
COUNT(CASE WHEN p.performance_score < 3.5 THEN 1 END) AS score_below_3_5_count
FROM public.performance p
JOIN public.department d ON p.department_id = d.department_id
GROUP BY d.department_name
ORDER BY score_5_0_count DESC, score_below_3_5_count DESC
limit 1;
-- SELECT d.department_name,
SUM(CASE WHEN p.performance_score = 5.0 THEN 1 ELSE 0 END) AS perfect_score_employees,
SUM(CASE WHEN p.performance_score < 3.5 THEN 1 ELSE 0 END) AS low_score_employees
FROM performance p
JOIN department d ON p.department_id = d.department_id
GROUP BY d.department_name
ORDER BY low_score_employees DESC, perfect_score_employees DESC;
--Q4 What is the average performance score by department?
SELECT
d.department_name,
ROUND(AVG(p.performance_score), 2) AS avg_performance_score
FROM performance p
JOIN department d ON p.department_id = d.department_id
GROUP BY d.department_name
ORDER BY avg_performance_score DESC;
------SALARY------
--Q1 What is the total salary expense for the company?
--SELECT SUM(salary_amount) AS total_salary_expense
FROM salary;
SELECT TO_CHAR(SUM(salary_amount), 'FM$999,999,999,999') AS "Total Salary Expense"
FROM salary;
---Q2 What is the average salary by job title?
SELECT e.job_title, TO_CHAR(ROUND(AVG(s.salary_amount), 2), 'FM$999,999,999.00') AS average_salary
FROM employee e
JOIN salary s ON s.employee_id = e.employee_id
GROUP BY e.job_title
ORDER BY AVG(s.salary_amount) DESC;
-- SELECT e.job_title,
ROUND(AVG(s.salary_amount), 2) AS avg_salary
FROM employee e
JOIN salary s ON e.employee_id = s.employee_id
GROUP BY e.job_title
ORDER BY avg_salary DESC;
---Q3 How many employees earn above 80,000?
SELECT COUNT(*) AS employees_above_80k
FROM salary
WHERE salary_amount > 80000;
---Q4 How does performance correlate with salary across departments?
SELECT
d.department_name,
TO_CHAR(ROUND(AVG(s.salary_amount), 2), 'FM$999,999,999.00') AS avg_salary,
ROUND(AVG(p.performance_score), 2) AS avg_performance
FROM employee e
JOIN salary s ON e.employee_id = s.employee_id
JOIN performance p ON e.employee_id = p.employee_id
JOIN department d ON e.department_id = d.department_id
GROUP BY d.department_name
ORDER BY avg_salary DESC;