-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSQLQuery_.sql
More file actions
318 lines (247 loc) · 10.7 KB
/
SQLQuery_.sql
File metadata and controls
318 lines (247 loc) · 10.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
/*
# \# SQL Project: Analyse Employee Data
<span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">This project uses the <code>zingo</code> database to explore employee data. </span>
We'll answer real-world questions using SQL queries, such as:
\- Listing employees in the Engineering department
\- Finding the highest paid employees
\- Counting employees per department
\- And more...
Let's begin!
*/
/*
### Connect to the Zingo Database and display tables
*/
USE zingo;
GO
SELECT * FROM sys.tables;
/*
**1\. List all employees** who work in the Engineering department.
*/
SELECT E.*,D.DeptName
FROM Employees E
JOIN Departments D ON E.DepartmentID = D.DepartmentID
WHERE D.DeptName = 'Engineering';
/*
**2\. Find all employees** hired after January 1, 2020.
*/
SELECT *
FROM Employees
where HireDate > '2020-01-01';
/*
**3\. Show employees** <span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">whose salary is between 50,000 and 90,000.</span>
*/
SELECT *
FROM Employees
WHERE Salary BETWEEN 50000 AND 90000;
/*
<span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">4. List all employees ordered by </span> **hire date (oldest first)**<span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">.</span>
*/
SELECT *
FROM Employees
Order by HireDate ASC;
/*
Show top 10 employees with the **highest salaries**.
*/
SELECT Distinct FirstName, Salary
FROM Employees
ORDER BY Salary DESC
OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY;
/*
But here I can See the dupliacte records, lets remove dupliacte records
*/
SELECT FirstName, LastName, Salary, DepartmentID, COUNT(*) AS DuplicateCount
FROM Employees
GROUP BY FirstName, LastName, Salary, DepartmentID
HAVING COUNT(*) > 1
ORDER BY DuplicateCount DESC;
/*
So we have these employess record duplicate. Lets Delete the duplicate records.
*/
DELETE e
FROM Employees e
JOIN (
SELECT FirstName, LastName, Salary, DepartmentID
FROM Employees
GROUP BY FirstName, LastName, Salary, DepartmentID
HAVING COUNT(*) > 1
) dup
ON e.FirstName = dup.FirstName
AND e.LastName = dup.LastName
AND e.Salary = dup.Salary
AND e.DepartmentID = dup.DepartmentID;
/*
Lets check if the duplicate records are deleted or still here.
*/
SELECT FirstName, LastName, Salary, DepartmentID, COUNT(*) AS DuplicateCount
FROM Employees
GROUP BY FirstName, LastName, Salary, DepartmentID
HAVING COUNT(*) > 1
ORDER BY DuplicateCount DESC;
/*
We have remove the duplicate records lets find <span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">top 10 employees with the</span> **highest salaries**<span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">.</span>
*/
SELECT Distinct FirstName, Salary
FROM Employees
ORDER BY Salary DESC
OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY;
/*
<span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">6. List employees in the </span> **Marketing department**<span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">, sorted by</span> **salary descending**<span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">.</span>
*/
SELECT E.*,D.DeptName
FROM Employees E
JOIN Departments D ON E.DepartmentID = D.DepartmentID
WHERE D.DeptName = 'Marketing'
ORDER BY Salary DESC;
/*
7\. Lets find the **unique department IDs** <span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">from the Employees table with Department name from Department table using join.</span>
*/
SELECT
E.FirstName,
E.LastName,
E.Salary,
D.DeptName,
AVG(E.Salary) OVER (PARTITION BY E.DepartmentID) AS AvgDeptSalary
FROM Employees E
JOIN Departments D ON E.DepartmentID = D.DepartmentID
WHERE D.DeptName = 'Sales'
ORDER BY E.Salary DESC;
/*
8\. Let's <span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">Find the </span> **average salary** <span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">of employees in the Sales department.</span>
*/
SELECT
E.FirstName,
E.LastName,
E.Salary,
D.DeptName,
AVG(E.Salary) OVER (PARTITION BY E.DepartmentID) AS AvgDeptSalary
FROM Employees E
JOIN Departments D ON E.DepartmentID = D.DepartmentID
WHERE D.DeptName = 'Sales'
ORDER BY E.Salary DESC;
/*
<span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">9. Let's Count how many employees each </span> **department** <span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">has.</span>
*/
SELECT D.DeptName, COUNT(*) AS EmployeeCount
FROM Employees E
JOIN Departments D ON E.DepartmentID = D.DepartmentID
GROUP BY D.DeptName
ORDER BY EmployeeCount DESC; -- optional: sorts by largest department
/*
10. Show each department with the **total salary payout** (group by DepartmentID)
*/
SELECT DepartmentID, SUM(Salary) AS TotalSalaryPayout
FROM Employees
GROUP BY DepartmentID;
/*
<span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">11. List departments where the </span> **average salary is over 80,000**<span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">.</span>
*/
SELECT DepartmentID, AVG(Salary) AS AvgSalary
FROM Employees
GROUP BY DepartmentID
HAVING AVG(Salary) > 80000;
/*
12. <span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">Count departments with </span> **more than 5 employees**<span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">.</span>
*/
SELECT DepartmentID, COUNT(*) AS EmployeeCount
FROM Employees
GROUP BY DepartmentID
HAVING COUNT(*) > 5;
/*
13. <span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">Show </span> **Employee Name, DeptName, and Salary** <span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">using a JOIN.</span>
*/
SELECT E.FirstName, E.LastName, D.DeptName, E.Salary
FROM Employees E
JOIN Departments D
ON E.DepartmentID = D.DepartmentID;
/*
14. <span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">Show all departments </span> **even if they have no employees** <span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">(LEFT JOIN).</span>
*/
SELECT D.DeptName, E.FirstName, E.LastName, E.Salary
FROM Departments D
LEFT JOIN Employees E
ON D.DepartmentID = E.DepartmentID;
/*
<span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">15. Show employee details for those in the </span> **IT** <span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">department.</span>
*/
SELECT E.*, D.DeptName
FROM Employees E
JOIN Departments D
ON E.DepartmentID = D.DepartmentID
Where DeptName = 'IT';
/*
16. <span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">Add a column showing </span> **Max salary** <span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">across all employees next to each row.</span>
*/
SELECT FirstName, LastName, DepartmentID, Salary,
MAX(Salary) OVER () AS Max_Salary
FROM Employees;
/*
Let's join the `Departments` table to the `Employees` table to add and display the department name (`DeptName`) for each employee.
*/
select E.FirstName, E.LastName,E.DepartmentID,E.Salary,D.DeptName,Max(E.Salary) Over() AS Max_Salary
FROM Employees E
Join Departments D
ON E.DepartmentID = D.DepartmentID;
/*
<span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">17. Add a column showing </span> **Max salary** <span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">across all employees next to each row.</span>
*/
SELECT FirstName, LastName, DepartmentID, Salary, MAX(Salary) OVER (PARTITION by (DepartmentID)) AS Max_Department_Salary
From Employees;
/*
18. <span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">Add a column to show </span> **employee rank by salary** <span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">within each department. also show thw dept name from departmant table. </span>
*/
SELECT
E.*,
D.DeptName,
rank() OVER (
Partition by (E.DepartmentID)
oRDER BY E.Salary DESC
) AS SALARY_RANK
FROM Employees E
Join Departments D
ON E.DepartmentID=D.DepartmentID;
/*
<span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">19. Show the </span> **top 2 highest paid employees** <span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">in each department.</span>
*/
SELECT *
FROM
(
SELECT
E.FirstName,
E.LastName,
E.Salary,
D.DeptName,
rank() OVER (
Partition by (E.DepartmentID)
oRDER BY E.Salary DESC
) AS SALARY_RANK
FROM Employees E
Join Departments D
ON E.DepartmentID=D.DepartmentID
) AS RANKED_EMPLOYEES
WHERE Salary_Rank<=2;
/*
<span style="font-family: -apple-system, BlinkMacSystemFont, sans-serif; color: var(--vscode-foreground);">20. Write a query that finds employees with </span> **duplicate first name.**
*/
/*
If we want just the Name count we can simply use group by and having with count function.
*/
SELECT FirstName, COUNT(*) AS NameCount
FROM Employees
GROUP BY FirstName
HAVING COUNT(*) > 1;
/*
But if we want to display all the details of the employees with dupliacte FirstName we can use the following query.
*/
SELECT *,COUNT(*) OVER (PARTITION BY FirstName) AS NameCount
FROM Employees
WHERE FirstName IN (
SELECT FirstName
FROM Employees
GROUP BY FirstName
HAVING COUNT(*) > 1
);
/*
<span style="background-color: transparent; color: rgb(26, 28, 30); font-family: "Helvetica Neue", sans-serif; font-size: 10.5pt; white-space-collapse: preserve;">21. Create a new column that shows the year only from the HireDate.</span>
*/
SELECT *, YEAR(HireDate) AS Year_Hired
FROM Employees;