-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL_Clauses_2.sql
More file actions
390 lines (303 loc) · 15.2 KB
/
SQL_Clauses_2.sql
File metadata and controls
390 lines (303 loc) · 15.2 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
-- SQL Clauses Part 2
-- NOTE: Before starting group by clause, first we will see what are the Aggregate Functions
USE SQL_Server_DB
CREATE TABLE Employee_Department (
Emp_ID INT PRIMARY KEY IDENTITY,
Emp_Name VARCHAR(100),
Emp_Age INT,
Emp_Email VARCHAR(100),
Emp_Date_of_Joining DATE,
Emp_Salary DECIMAL(10, 2),
Emp_Department VARCHAR(50)
);
INSERT INTO Employee_Department (Emp_Name, Emp_Age, Emp_Email, Emp_Date_of_Joining, Emp_Salary, Emp_Department)
VALUES
('John Doe', 30, 'john.doe@example.com', '2020-01-15', 50000.00, 'IT'),
('Jane Smith', 28, 'jane.smith@example.com', '2019-03-22', 55000.50, 'HR'),
('Emily Johnson', 30, 'emily.johnson@example.com', '2017-09-15', 60000.00, 'IT'),
('Robert Brown', 35, 'robert.brown@example.com', '2018-07-01', 62000.75, 'Finance'),
('David Williams', 40, 'david.williams@example.com', '2016-11-01', 70000.00, 'Finance'),
('Sarah Miller', 28, 'sarah.miller@example.com', '2021-06-05', 50000.00, 'HR'),
('Michael Taylor', 40, 'michael.taylor@example.com', '2020-08-20', 47000.50, 'IT'),
('Laura Anderson', 38, 'laura.anderson@example.com', '2015-12-10', 75000.55, 'Marketing'),
('James Thomas', 30, 'james.thomas@example.com', '2020-01-15', 50000.00, 'IT'),
('Sophia Jackson', 40, 'sophia.jackson@example.com', '2021-06-05', 60000.00, 'HR')
INSERT INTO Employee_Department (Emp_Name, Emp_Email, Emp_Date_of_Joining, Emp_Department)
VALUES ('James Jackson', 'james.jackson@example.com', '2019-03-22', 'Marketing')
SELECT * FROM Employee_Department
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- AGGREGATE FUNCTIONS
-- • COUNT(): Returns the number of rows that match a specified condition.
SELECT COUNT(Emp_Salary) AS Total_No FROM Employee_Department
SELECT COUNT(*) AS Total_Row FROM Employee_Department --COUNT(*) counts null value also
SELECT COUNT(Emp_Name) as Total_Names FROM Employee_Department
-- • SUM(): Returns the sum of the value in a numeric column.
SELECT SUM(Emp_Salary) AS Total_Salary FROM Employee_Department
-- • AVG(): Returns the average/mean value of a numeric column.
SELECT AVG(Emp_Age) as Average_Age FROM Employee_Department
SELECT AVG(Emp_Salary) AS Average_Salary FROM Employee_Department
-- • MIN(): Returns the minimum value in a set of values.
SELECT MIN(Emp_Age) AS Minimum_Age FROM Employee_Department
SELECT MIN(Emp_Salary) AS Minimum_Salary FROM Employee_Department
SELECT Emp_Name, Emp_Age FROM Employee_Department WHERE Emp_Age = (SELECT MIN(Emp_Age) AS Minimum_Age FROM Employee_Department)
SELECT Emp_Name, Emp_Salary FROM Employee_Department WHERE Emp_Salary = (SELECT MIN(Emp_Salary) AS Minimum_Salary FROM Employee_Department)
-- • MAX(): Return the maximum value in a set of values.
SELECT MAX(Emp_Age) AS Maximum_Age FROM Employee_Department
SELECT MAX(Emp_Salary) AS Maximum_Salary FROM Employee_Department
SELECT Emp_Name, Emp_Age FROM Employee_Department WHERE Emp_Age = (SELECT MAX(Emp_Age) AS Maximum_Age FROM Employee_Department)
SELECT Emp_Name, Emp_Salary FROM Employee_Department WHERE Emp_Salary = (SELECT MAX(Emp_Salary) AS Maximum_Salary FROM Employee_Department)
-- • DISTINCT(): Shows only distinct/unique values.
SELECT DISTINCT(Emp_Age) AS Distinct_Age FROM Employee_Department
SELECT DISTINCT(Emp_Name) AS Distinct_Names FROM Employee_Department
-- • TOP(): Top is used to restrict the number of rows returned from the query.
SELECT TOP(5) * FROM Employee_Department
SELECT TOP(3) Emp_Name FROM Employee_Department
-- NOTE: Aggregate functions ignore NULL values in the calculation, except for COUNT(*), which counts NULL values.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- 2. GROUP BY clause: Group rows that have the same values into summary rows, often used with Aggregate Functions.
-- Number of employees who is having same age.
SELECT Emp_Age, COUNT(*) AS Total_Age FROM Employee_Department GROUP BY Emp_Age
-- Calculate the average salary of employees in each department.
SELECT Emp_Department,
AVG(Emp_Salary) AS Group_Salary
FROM Employee_Department
GROUP BY Emp_Department
-- Count the number of employees in each department.
SELECT Emp_Department,
COUNT(*) AS No_of_Emp
FROM Employee_Department
GROUP BY Emp_Department
-- Find the total salary paid to employees in each department.
SELECT Emp_Department,
SUM(Emp_Salary) AS Total_Salary_Paid
FROM Employee_Department
GROUP BY Emp_Department
-- List the highest salary in each department.
SELECT Emp_Department,
MAX(Emp_Salary) as Highest_Salary
FROM Employee_Department
GROUP BY Emp_Department
-- List the number of employees who joined in each year.
SELECT YEAR(Emp_Date_of_Joining),
COUNT(*) AS No_of_Employee
FROM Employee_Department
GROUP BY YEAR(Emp_Date_of_Joining)
-- Find the average age of employees in each department.
SELECT Emp_Department,
AVG(Emp_Age) AS Average_Age
FROM Employee_Department
GROUP BY Emp_Department
-- Find the earliest hire date for employees in each department.
SELECT Emp_Department,
MIN(Emp_Date_of_Joining) AS Earliest_Hire_Date
FROM Employee_Department
GROUP BY Emp_Department
-- Count the number of employees who have a salary above 50,000 in each department.
SELECT Emp_Department,
COUNT(*) AS No_of_Employee
FROM Employee_Department
WHERE Emp_Salary > 50000
GROUP BY Emp_Department
-- Find the number of employees in each department who are older than 30.
SELECT Emp_Department,
COUNT(*) AS No_of_Employee
FROM Employee_Department
WHERE Emp_Age > 30
GROUP BY Emp_Department
-- List the department-wise average age and salary of employees.
SELECT Emp_Department,
AVG(Emp_Age) AS Average_Age,
AVG(Emp_Salary) AS Average_Salary
FROM Employee_Department
GROUP BY Emp_Department
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- 3. HAVING clause: Similar to WHERE, but it is used to filter results after aggregation (i.e., works with GROUP BY). It filters the results based on the condition applied to the aggregated data.
-- Find departments with more than 2 employees.
SELECT Emp_Department,
COUNT(*) AS No_of_Employee
FROM Employee_Department
GROUP BY Emp_Department
HAVING COUNT(*) > 2
-- Find departments where the average salary is greater than 60,000.
SELECT Emp_Department,
AVG(Emp_Salary) AS Average_Salary
FROM Employee_Department
GROUP BY Emp_Department
HAVING AVG(Emp_Salary) > 60000
-- List departments where the total salary is greater than 200,000.
SELECT Emp_Department,
SUM(Emp_Salary) AS Total_Salary
FROM Employee_Department
GROUP BY Emp_Department
HAVING SUM(Emp_Salary) > 200000
-- Find departments with at least one employee having a salary above 70,000.
SELECT Emp_Department,
MAX(Emp_Salary) AS No_of_Employee
FROM Employee_Department
GROUP BY Emp_Department
HAVING MAX(Emp_Salary) > 70000
--or
SELECT Emp_Department,
MAX(Emp_Salary) AS No_of_Employee
FROM Employee_Department
WHERE Emp_Salary > 70000
GROUP BY Emp_Department
-- Find departments where the number of employees with a salary above 50,000 is greater than 1.
SELECT Emp_Department,
COUNT(*) AS No_of_Employee
FROM Employee_Department
WHERE Emp_Salary > 50000
GROUP BY Emp_Department
HAVING COUNT(*) > 1
-- List departments where the number of employees who joined after 2019 is more than 2.
SELECT Emp_Department,
COUNT(*) AS No_of_Employee
FROM Employee_Department
WHERE YEAR(Emp_Date_of_Joining) > 2019
GROUP BY Emp_Department
HAVING COUNT(*) > 2
-- Find departments where the average salary is between 50,000 and 60,000.
SELECT Emp_Department,
AVG(Emp_Salary) as Average_Salary
FROM Employee_Department
GROUP BY Emp_Department
HAVING AVG(Emp_Salary) BETWEEN 50000 AND 60000
-- List departments where the total salary exceeds 200,000 but has fewer than 5 employees.
SELECT Emp_Department,
SUM(Emp_Salary) as Total_Salary,
COUNT(*) AS No_of_Employee
FROM Employee_Department
GROUP BY Emp_Department
HAVING AVG(Emp_Salary) > 200000 and COUNT(*) < 5 --no recors because sum(emp_salary) is not greater than 300000, its below 300000 but query is correct
-- Find departments where the maximum employee age is greater than 39.
SELECT Emp_Department,
MAX(Emp_Age) AS Maximum_Age
FROM Employee_Department
GROUP BY Emp_Department
HAVING MAX(Emp_Age)>39
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- 4. ORDER BY clause: Orders the result set based on one or more columns, either in ascending (ASC) or descending (DESC) order.
-- List all employees sorted by their age in ascending order.
SELECT Emp_ID, Emp_Name, Emp_Age
FROM Employee_Department
ORDER BY Emp_Age
-- List all employees sorted by their salary in descending order.
SELECT Emp_ID, Emp_Name, Emp_Salary
FROM Employee_Department
ORDER BY Emp_Salary DESC
-- List all employees sorted by their date of joining, starting from the most recent.
SELECT Emp_ID, Emp_Name, Emp_Date_of_Joining
FROM Employee_Department
ORDER BY Emp_Date_of_Joining DESC
-- List all employees in the IT department, sorted by their salary in ascending order.
SELECT Emp_Name, Emp_Salary, Emp_Department
FROM Employee_Department
WHERE Emp_Department = 'IT'
ORDER BY Emp_Salary
-- List all employees sorted by their name in alphabetical order.
SELECT Emp_ID, Emp_Name
FROM Employee_Department
ORDER BY Emp_Name
-- List employees sorted by department first, and then by salary within each department in descending order.
SELECT Emp_Name, Emp_Department, Emp_Salary
FROM Employee_Department
ORDER BY Emp_Department, Emp_Salary DESC
-- List employees sorted by their age in descending order, and within the same age, by their date of joining in ascending order.
SELECT Emp_ID, Emp_Age, Emp_Date_of_Joining
FROM Employee_Department
ORDER BY Emp_Age DESC, Emp_Date_of_Joining
-- List employees whose salary is above 50,000, sorted by their name in alphabetical order.
SELECT Emp_ID, Emp_Name, Emp_Salary
FROM Employee_Department
WHERE Emp_Salary > 50000
ORDER BY Emp_Name
-- List the employees in the Finance department, sorted by their joining date in ascending order and age in descending order.
SELECT Emp_ID, Emp_Date_of_Joining, Emp_Age, Emp_Department
FROM Employee_Department
WHERE Emp_Department = 'Finance'
ORDER BY Emp_Date_of_Joining, Emp_Age DESC
-- List the employees who are younger than 30, sorted by salary in descending order.
SELECT Emp_Age, Emp_Salary
FROM Employee_Department
WHERE Emp_Age > 30
ORDER BY Emp_Salary DESC
-- Find the number of employees in each department and order the result by the number of employees in descending order.
SELECT Emp_Department,
COUNT(*) AS NO_of_Employee
FROM Employee_Department
GROUP BY Emp_Department
ORDER BY NO_of_Employee DESC
-- Find the average salary of employees in each department, and only include departments where the average salary is greater than 50,000, ordered by average salary in descending order.
SELECT Emp_Department,
AVG(Emp_Salary) AS Avgerage_Salary
FROM Employee_Department
GROUP BY Emp_Department
HAVING AVG(Emp_Salary) > 50000
ORDER BY Avgerage_Salary DESC
-- List departments where the total salary of employees is more than 200,000, and order the results by total salary in descending order.
SELECT Emp_Department,
SUM(Emp_Salary) AS Total_Salary
FROM Employee_Department
GROUP BY Emp_Department
HAVING SUM(Emp_Salary) > 200000
ORDER BY Total_Salary DESC
-- Find departments where the average salary is between 50,000 and 60,000, and order the results by department name in alphabetical order.
SELECT Emp_Department,
AVG(Emp_Salary) AS Average_Salary
FROM Employee_Department
GROUP BY Emp_Department
HAVING AVG(Emp_Salary) BETWEEN 50000 AND 60000
ORDER BY Emp_Department
-- Find departments where the number of employees with a salary above 50,000 is greater than 1, and order the results by the number of such employees in descending order.
SELECT Emp_Department,
COUNT(*) AS No_of_Employee
FROM Employee_Department
WHERE Emp_Salary > 50000
GROUP BY Emp_Department
HAVING COUNT(*) > 1
ORDER BY No_of_Employee DESC
-- List the departments where the average age of employees is greater than 30, and order the results by the average age in ascending order.
SELECT Emp_Department,
AVG(Emp_Age) AS Average_Age
FROM Employee_Department
GROUP BY Emp_Department
HAVING AVG(Emp_Age) > 30
ORDER BY Average_Age
-- Find departments where the maximum salary is greater than 70,000, and order the results by the maximum salary in descending order.
SELECT Emp_Department,
MAX(Emp_Salary) AS Maximum_Salary
FROM Employee_Department
GROUP BY Emp_Department
HAVING MAX(Emp_Salary) > 70000
ORDER BY Maximum_Salary DESC
-- List departments where the number of employees is more than 2 and the total salary exceeds 150,000, ordered by total salary in ascending order.
SELECT Emp_Department,
COUNT(*) AS No_of_Employee,
SUM(Emp_Salary) as Total_Salary
FROM Employee_Department
GROUP BY Emp_Department
HAVING COUNT(*) > 2 AND SUM(Emp_Salary) > 150000
ORDER BY Total_Salary
-- Find departments where the total number of employees with salary above 50,000 is greater than 1, and order by the department name alphabetically.
SELECT Emp_Department,
COUNT(*) AS Total_Employee
FROM Employee_Department
WHERE Emp_Salary > 50000
GROUP BY Emp_Department
HAVING COUNT(*) > 1
ORDER BY Emp_Department
-- List the departments where the average salary is less than 60,000 and the number of employees is more than 3, ordered by department name.
SELECT Emp_Department,
AVG(Emp_Salary) AS Average_Salary,
COUNT(*) AS No_of_Employee
FROM Employee_Department
GROUP BY Emp_Department
HAVING AVG(Emp_Salary) < 60000 AND COUNT(*) > 3
ORDER BY Emp_Department
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- 5. LIMIT/TOP clause: Limits the number of rows returned in the result set.
-- NOTE: LIMIT clause works in MySQL, PostgreSQL, SQLite and some other database. TOP clause works in SQLServer
SELECT TOP 5 Emp_Name, Emp_Salary FROM Employee_Department ORDER BY Emp_Salary
SELECT TOP 5 Emp_Name, Emp_Salary FROM Employee_Department ORDER BY Emp_Salary DESC
SELECT TOP 3 * FROM Employee_Department
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------