-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopQuestionsPractice.sql
More file actions
131 lines (89 loc) · 3.02 KB
/
topQuestionsPractice.sql
File metadata and controls
131 lines (89 loc) · 3.02 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
1.Find the second highest salary from the Employee table.
SELECT MAX(salary)
FROM employee
WHERE salary < (SELECT MAX(salary) FROM employee);
------x--------x--------x---------x---------x-------
2.Find employees who earn more than their manager.
SELECT e.name
FROM employee e
JOIN employee m ON e.manager_id = m.id
WHERE e.salary > m.salary;
------x--------x--------x---------x---------x-------
3.Find duplicate emails in a users table.
SELECT email, COUNT(*)
FROM users
GROUP BY email
HAVING COUNT(*) > 1;
------x--------x--------x---------x---------x-------
4.List all departments that have more than 5 employees.
SELECT department_id, COUNT(*) AS total_employees
FROM employee
GROUP BY department_id
HAVING COUNT(*) > 5;
------x--------x--------x---------x---------x-------
5.Find customers who never placed an order.
SELECT c.name
FROM customers c
LEFT JOIN orders o ON c.id = o.customer_id
WHERE o.id IS NULL;
------x--------x--------x---------x---------x-------
6.Display employee name and department name (using JOIN).
SELECT e.name, d.name AS department
FROM employee e
JOIN department d ON e.department_id = d.id;
------x--------x--------x---------x---------x-------
7.Get the nth highest salary (e.g., 3rd highest).
SELECT DISTINCT salary
FROM employee e1
WHERE 3 = (
SELECT COUNT(DISTINCT salary)
FROM employee e2
WHERE e2.salary >= e1.salary
);
------x--------x--------x---------x---------x-------
8.Get the list of employees who joined in the last 30 days.
SELECT *
FROM employee
WHERE join_date >= CURDATE() - INTERVAL 30 DAY;
------x--------x--------x---------x---------x-------
9.Get employees who belong to more than one project.
SELECT employee_id, COUNT(DISTINCT project_id) AS project_count
FROM employee_project
GROUP BY employee_id
HAVING COUNT(DISTINCT project_id) > 1;
------x--------x--------x---------x---------x-------
10.Write a query to fetch employees with same salary.
SELECT salary
FROM employee
GROUP BY salary
HAVING COUNT(*) > 1;
------x--------x--------x---------x---------x-------
11.List the top 5 highest paid employees.
SELECT *
FROM employee
ORDER BY salary DESC
LIMIT 5;
------x--------x--------x---------x---------x-------
12.Find employees whose name starts and ends with 'a'.
SELECT *
FROM employee
WHERE name LIKE 'a%' AND name LIKE '%a';
------x--------x--------x---------x---------x-------
13.Get the department-wise highest paid employee.
SELECT e.*
FROM employee e
JOIN (
SELECT department_id, MAX(salary) AS max_salary
FROM employee
GROUP BY department_id
) d ON e.department_id = d.department_id AND e.salary = d.max_salary;
------x--------x--------x---------x---------x-------
14.Display all employee names in one row separated by commas.
SELECT GROUP_CONCAT(name SEPARATOR ', ') AS all_employees
FROM employee;
------x--------x--------x---------x---------x-------
15.Swap two column values (e.g., salary and bonus) in a table.
UPDATE employee
SET salary = salary + bonus,
bonus = salary - bonus,
salary = salary - bonus;