-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubQueries.sql
More file actions
361 lines (280 loc) · 6.27 KB
/
SubQueries.sql
File metadata and controls
361 lines (280 loc) · 6.27 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
# Basic Level
#1 Retrieve the names of employees who earn more than the average salary of all employees.
create database newdatabase_db;
use newdatabase_db;
CREATE TABLE Employee (
emp_id INT PRIMARY KEY,
name VARCHAR(50),
department_id VARCHAR(10),
salary INT
);
INSERT INTO Employee (emp_id, name, department_id, salary) VALUES
(101, 'Abhishek', 'D01', 62000),
(102, 'Shubham', 'D01', 58000),
(103, 'Priya', 'D02', 67000),
(104, 'Rohit', 'D02', 64000),
(105, 'Neha', 'D03', 72000),
(106, 'Aman', 'D03', 55000),
(107, 'Ravi', 'D04', 60000),
(108, 'Sneha', 'D04', 75000),
(109, 'Kiran', 'D05', 70000),
(110, 'Tanuja', 'D05', 65000);
SELECT name
FROM Employee
WHERE salary > (
SELECT AVG(salary)
FROM Employee
);
#2 Find the employees who belong to the department with the highest average salary.
SELECT department_id, AVG(salary)
FROM Employee
GROUP BY department_id;
SELECT MAX(avg_salary)
FROM (
SELECT AVG(salary) AS avg_salary
FROM Employee
GROUP BY department_id
) t;
SELECT department_id
FROM Employee
GROUP BY department_id
HAVING AVG(salary) = (
SELECT MAX(avg_salary)
FROM (
SELECT AVG(salary) AS avg_salary
FROM Employee
GROUP BY department_id
) t
);
SELECT name
FROM Employee
WHERE department_id = (
SELECT department_id
FROM Employee
GROUP BY department_id
HAVING AVG(salary) = (
SELECT MAX(avg_salary)
FROM (
SELECT AVG(salary) AS avg_salary
FROM Employee
GROUP BY department_id
)t
)
);
#3 List all employees who have made at least one sale.
CREATE TABLE Sales (
sale_id INT PRIMARY KEY,
emp_id INT,
sale_amount INT,
sale_date DATE
);
INSERT INTO Sales (sale_id, emp_id, sale_amount, sale_date) VALUES
(201, 101, 4500, '2025-01-05'),
(202, 102, 7800, '2025-01-10'),
(203, 103, 6700, '2025-01-14'),
(204, 104, 12000, '2025-01-20'),
(205, 105, 9800, '2025-02-02'),
(206, 106, 10500, '2025-02-05'),
(207, 107, 3200, '2025-02-09'),
(208, 108, 5100, '2025-02-15'),
(209, 109, 3900, '2025-02-20'),
(210, 110, 7200, '2025-03-01');
SELECT emp_id FROM Sales;
SELECT name
FROM Employee
WHERE emp_id IN (
SELECT emp_id
FROM Sales
);
#4 Find the employee with the highest sale amount.
SELECT MAX(sale_amount)
FROM Sales;
SELECT emp_id
FROM Sales
WHERE sale_amount = (
SELECT MAX(sale_amount)
FROM Sales
);
SELECT name
FROM Employee
WHERE emp_id = (
SELECT emp_id
FROM Sales
WHERE sale_amount = (
SELECT MAX(sale_amount)
FROM Sales
)
);
#5 Retrieve the names of employees whose salaries are higher than Shubham’s salary.
SELECT salary
FROM Employee
WHERE name = 'Shubham';
SELECT name
FROM Employee
WHERE salary > (
SELECT salary
FROM Employee
WHERE name = 'Shubham'
);
# Intermediate Level
#1 Find employees who work in the same department as Abhishek.
SELECT department_id
FROM Employee
WHERE name = 'Abhishek';
SELECT name
FROM Employee
WHERE department_id = (
SELECT department_id
FROM Employee
WHERE name = 'Abhishek'
);
SELECT name
FROM Employee
WHERE department_id = (
SELECT department_id
FROM Employee
WHERE name = 'Abhishek'
)
AND name <> 'Abhishek';
#2 List departments that have at least one employee earning more than ₹60,000.
SELECT DISTINCT department_id
FROM Employee
WHERE salary > 60000;
SELECT department_name
FROM Department
WHERE department_id IN (
SELECT DISTINCT department_id
FROM Employee
WHERE salary > 60000
);
#3 Find the department name of the employee who made the highest sale.
SELECT MAX(sale_amount)
FROM Sales;
SELECT emp_id
FROM Sales
WHERE sale_amount = (
SELECT MAX(sale_amount)
FROM Sales
);
SELECT department_name
FROM Department
WHERE department_id = (
SELECT department_id
FROM Employee
WHERE emp_id = (
SELECT emp_id
FROM Sales
WHERE sale_amount = (
SELECT MAX(sale_amount)
FROM Sales
)
)
);
#4 Retrieve employees who have made sales greater than the average sale amount.
SELECT AVG(sale_amount)
FROM Sales;
SELECT emp_id
FROM Sales
WHERE sale_amount > (
SELECT AVG(sale_amount)
FROM Sales
);
SELECT name
FROM Employee
WHERE emp_id IN (
SELECT emp_id
FROM Sales
WHERE sale_amount > (
SELECT AVG(sale_amount)
FROM Sales
)
);
#5 Find the total sales made by employees who earn more than the average salary.
SELECT AVG(salary)
FROM Employee;
SELECT emp_id
FROM Employee
WHERE salary > (
SELECT AVG(salary)
FROM Employee
);
SELECT SUM(sale_amount) AS total_sales
FROM Sales
WHERE emp_id IN (
SELECT emp_id
FROM Employee
WHERE salary > (
SELECT AVG(salary)
FROM Employee
)
);
#Advanced Level
#1 Find employees who have not made any sales.
SELECT emp_id
FROM Sales;
SELECT name
FROM Employee
WHERE emp_id NOT IN (
SELECT emp_id
FROM Sales
);
#2 List departments where the average salary is above ₹55,000.
SELECT department_id
FROM Employee
GROUP BY department_id
HAVING AVG(salary) > 55000;
SELECT department_name
FROM Department
WHERE department_id IN (
SELECT department_id
FROM Employee
GROUP BY department_id
HAVING AVG(salary) > 55000
);
#3 Retrieve department names where the total sales exceed ₹10,000.
SELECT e.department_id
FROM Employee e
JOIN Sales s ON e.emp_id = s.emp_id
GROUP BY e.department_id
HAVING SUM(s.sale_amount) > 10000;
SELECT department_name
FROM Department
WHERE department_id IN (
SELECT e.department_id
FROM Employee e
JOIN Sales s ON e.emp_id = s.emp_id
GROUP BY e.department_id
HAVING SUM(s.sale_amount) > 10000
);
#4 Find the employee who has made the second-highest sale.
SELECT MAX(sale_amount)
FROM Sales;
SELECT MAX(sale_amount)
FROM Sales
WHERE sale_amount < (
SELECT MAX(sale_amount)
FROM Sales
);
SELECT name
FROM Employee
WHERE emp_id = (
SELECT emp_id
FROM Sales
WHERE sale_amount = (
SELECT MAX(sale_amount)
FROM Sales
WHERE sale_amount < (
SELECT MAX(sale_amount)
FROM Sales
)
)
);
#5 Retrieve the names of employees whose salary is greater than the highest sale amount recorded.
SELECT MAX(sale_amount)
FROM Sales;
SELECT name
FROM Employee
WHERE salary > (
SELECT MAX(sale_amount)
FROM Sales
);