-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday5SQLQuery1nestedqueries.sql
More file actions
133 lines (99 loc) · 2.68 KB
/
day5SQLQuery1nestedqueries.sql
File metadata and controls
133 lines (99 loc) · 2.68 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
--- USE DATABASE
USE SQL_ANJALI_SESSION;
--- SESSION 3 : SUBQUERIES (NESTED QUERIES)
--- Employee table creation for subquery practice
CREATE TABLE nestedqueries
(
EID INT,
Ename VARCHAR(30),
Esalary INT
);
--- Inserting data
--- Two employees have the SAME highest salary (60000)
INSERT INTO nestedqueries VALUES
(1,'Amit',40000),
(2,'Neha',55000),
(3,'Ravi',60000),
(4,'Pooja',60000),
(5,'Arjun',45000);
--- View all records
SELECT * FROM nestedqueries;
--- Finding maximum salary
SELECT MAX(Esalary) FROM nestedqueries;
--- Using TOP 1 (out of the box approach)
SELECT TOP 1 EID, Ename, Esalary
FROM nestedqueries
ORDER BY Esalary DESC;
--- Using SUBQUERY (important interview question)
SELECT *
FROM nestedqueries
WHERE Esalary = (SELECT MAX(Esalary) FROM nestedqueries);
--- FINDING SECOND HIGHEST SALARY
SELECT MAX(Esalary) AS Second_Highest_Salary
FROM nestedqueries
WHERE Esalary < (SELECT MAX(Esalary) FROM nestedqueries);
--- Getting employee details having second highest salary
SELECT *
FROM nestedqueries
WHERE Esalary =
(
SELECT MAX(Esalary)
FROM nestedqueries
WHERE Esalary < (SELECT MAX(Esalary) FROM nestedqueries)
);
--- Find all employees whose salary is higher than the average salary
SELECT Ename, Esalary
FROM nestedqueries
WHERE Esalary > (SELECT AVG(Esalary) FROM nestedqueries);
--- Generic example (COMMENTED to avoid error – table not created)
-- SELECT *
-- FROM Products
-- WHERE Price > (SELECT AVG(Price) FROM Products);
--- Drop table after usage
DROP TABLE nestedqueries;
---------------------------------------------------------
--- NEW TABLE 2 : EMPLOYEE
---------------------------------------------------------
CREATE TABLE Employeeee
(
EID INT,
Ename VARCHAR(30),
Department VARCHAR(30),
Age INT,
Esalary INT
);
INSERT INTO Employeeee VALUES
(1,'Amit','IT',28,50000),
(2,'Neha','HR',32,60000),
(3,'Ravi','IT',35,60000),
(4,'Pooja','HR',26,45000),
(5,'Arjun','Sales',40,55000);
--- QUERY 1 (COMMENTED – Sales table not created)
-- SELECT DISTINCT ProductName
-- FROM Sales
-- WHERE ProductName IN
-- (
-- SELECT ProductName
-- FROM Sales
-- WHERE Region = 'Vadodara'
-- );
--- QUERY 2
--- Find departments having at least one employee older than 30
SELECT DISTINCT Department
FROM Employeeee
WHERE Department IN
(
SELECT Department
FROM Employeeee
WHERE Age > 30
);
--- QUERY 3
--- Employees earning max salary in their department
SELECT Ename, Department, Esalary
FROM Employeeee
WHERE Esalary IN
(
SELECT MAX(Esalary)
FROM Employeeee
GROUP BY Department
);