-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSql-Practise.sql
283 lines (229 loc) · 8.01 KB
/
Sql-Practise.sql
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
CREATE DATABASE SQL_Practice;
################################## Create Employee Table ######################
create table Employee
(
empID INT AUTO_INCREMENT,
empFname varchar(20),
empLname varchar(20),
department varchar(20),
salary double,
genger varchar(10),
PRIMARY KEY (empID)
);
################################## Create Position Table ######################
create table Position
(
empID INT NOT NULL,
position varchar(20),
bonas double,
joiningDate DATE,
FOREIGN KEY (empID) REFERENCES Employee(empID)
);
################################# Inser Data into Employee #######################
INSERT INTO Employee (empFname,empLname,department,salary,genger)
values("Jhon", "Smith", "HR", 5000.00,"M");
INSERT INTO Employee (empFname,empLname,department,salary,genger)
values("Elsa", "Ferdinand", "Admin", 8000.00,"F");
INSERT INTO Employee (empFname,empLname,department,salary,genger)
values("Ronaldo", "Guzman", "Account", 3000.00,"M");
INSERT INTO Employee (empFname,empLname,department,salary,genger)
values("Suzan", "Alex", "HR", 7000.00,"F");
INSERT INTO Employee (empFname,empLname,department,salary,genger)
values("Sajin", "Guzman", "Security", 10000.00,"M");
INSERT INTO Employee (empFname,empLname,department,salary,genger)
values("Rodrigoz", "Garcia", "Engineer", 10000.00,"M");
INSERT INTO Employee (empFname,empLname,department,salary,genger)
values("Sashi", "Martinez", "HR", 5000.00,"M");
################################# Inser Data into Position #######################
INSERT INTO Position (empID,position,bonas,joiningDate)
values(1,"MANAGER",1000.00,"2000-01-01");
INSERT INTO Position (empID,position,bonas,joiningDate)
values(2,"EXCUTIVE",2000.00,"2010-01-01");
INSERT INTO Position (empID,position,bonas,joiningDate)
values(3,"LEAD",1500.00,"2016-02-01");
INSERT INTO Position (empID,position,bonas,joiningDate)
values(4,"MANAGER",1000.00,"2001-08-01");
INSERT INTO Position (empID,position,bonas,joiningDate)
values(5,"SUPER VISER",150.00,"2008-08-01");
INSERT INTO Position (empID,position,bonas,joiningDate)
values(6,"TEAM LEAD",600.00,"2012-02-09");
INSERT INTO Position (empID,position,bonas,joiningDate)
values(7,"TEAM LEAD",600.00,"2012-05-06");
INSERT INTO Position (empID,position,bonas,joiningDate)
values(8,"TEAM LEAD",600.00,"2012-03-03");
################################# View, Drop, Check Data #######################
-- To view records inside the table
SELECT * FROM Employee;
SELECT * FROM Position;
-- To Delete table
DROP TABLE Employee;
DROP TABLE Position;
-- To Delete All recods inside the table.
TRUNCATE TABLE Employee;
TRUNCATE TABLE Position;
################################# Query Problems #######################
-- Q1, Query empFname in upper Case and use alias nbame as Emplpyee Name.
SELECT UPPER(empFname) AS 'Employee Name'
From Employee;
-- Q2, Query number of Employee working in HR?
SELECT COUNT(empFname)
From Employee
WHERE DEPARTMENT = 'HR';
-- Q3, Query current date?
SELECT CURDATE();
-- 4, Query all Employee whose salary is between 5000 to 10000
SELECT *
FROM Employee
WHERE salary BETWEEN 5000.00 AND 10000.00;
-- Q5, Query name of Employee that begin with 'S'
SELECT *
FROM Employee
WHERE empFname LIKE 'S%';
-- Q6, Query 2 Employee with salary greater than 7000.00.
SELECT *
FROM Employee
WHERE salary > 7000.00
LIMIT 2;
-- Q7, Query All Employees with top salary?
SELECT *
FROM Employee
WHERE Salary = (SELECT MAX(Salary)
FROM Employee);
-- Q8, Query Employee with 2 top salary?
SELECT *
FROM Employee
ORDER BY Salary DESC LIMIT 2;
-- Q9, Query Employee first and last name separated by space as full name?
SELECT CONCAT(empFname, ' ' , empLname) AS 'Full Name'
FROM Employee;
-- Q10, Query all Employee ordered by last name decendently?
SELECT *
FROM Employee
ORDER BY empLname DESC;
-- Q11, Query all Employee except Sajin and Rodrigoz?
SELECT *
FROM Employee
WHERE empFname NOT IN ('Sajin', 'Rodrigoz');
-- Alternating way
SELECT *
FROM Employee
WHERE empFname != 'Sajin' 'Rodrigoz';
-- Q12, Write a query to fetch all Employees who also hold the managerial position.
-- Solution using JOIN statement
SELECT *
FROM Employee e , Position p
WHERE e.empID = p.empID AND p.position = 'MANAGER';
-- Alternating way using INNER JOIN, AND, & IN
SELECT *
FROM Employee e
INNER JOIN Position p
ON e.empID = p.empID AND p.position IN ('MANAGER');
-- Alternating way using INNER JOIN & WHERE
SELECT *
FROM Employee e
INNER JOIN Position p
ON e.empID = p.empID
WHERE p.posItion = 'MANAGER';
-- Q13, Write a query to retrieve employee details from Employee table who have a date of joining in the Postion table.
-- Solution using JOIN & EXISTS
SELECT *
FROM Employee e, Position p
WHERE EXISTS( SELECT * FROM Position WHERE e.empID = p.empID);
-- Alternating way using JOIN
SELECT *
FROM Employee e, Position p
WHERE e.empID = p.empID;
-- Q14, Write a query to retrieve two maximum bonas from the Position table.
-- Solution using colerated query
SELECT DISTINCT bonas FROM Position p
WHERE 2 >= (SELECT COUNT(DISTINCT bonas)
FROM Position p1
WHERE p1.bonas >= p.bonas) ORDER BY p.bonas DESC;
-- Q15, Write a query to retrieve two maximum bonas from the Position table.
-- Solution using colerated query
SELECT DISTINCT bonas FROM Position p
WHERE 2 >= (SELECT COUNT(DISTINCT bonas)
FROM Position p1
WHERE p1.bonas <= p.bonas) ORDER BY p.bonas DESC;
-- Q16, Write a query to find the 3rd highest salary from the table without using TOP/limit keyword.
SELECT DISTINCT salary FROM Employee e
WHERE 2 = (SELECT COUNT(DISTINCT salary)
FROM Employee e1
WHERE e1.salary > e.salary);
-- Q17, Write a query to retrieve duplicate records from a table.
SELECT empID, position, COUNT(*)
FROM Position
GROUP BY empID, position, bonas
HAVING COUNT(*) > 1;
-- Q18,Write a query to retrieve the list of employees working in the same department.
SELECT DISTINCT e.empID, e.empFname, e.department
FROM Employee e, Employee e1
WHERE e.department = e1.Department AND e.empID != e1.empID
ORDER BY department;
-- Alternating way using IN
SELECT empID, empFname,department
FROM Employee
WHERE department IN (SELECT department
FROM Employee
GROUP BY department
HAVING COUNT(*) > 1);
-- Order Position
-- With non duplicate ID
-- Q19, Write a query to retrieve the last 3 records from the EmployeeInfo table(No duplicate ID)
(SELECT *
FROM Position
ORDER BY empID DESC
LIMIT 3)
ORDER BY empID ASC;
-- Alternating way using count which return single number
SELECT *
FROM Employee
WHERE empID > (SELECT COUNT(*) FROM Employee) - 3;
-- Common Solution Works for Many Problems --
-- Max & Min With dublicate value
-- Q20, Write a query to find the third-highest salary from the Employee table.
-- Solution using Limit & offset. It works for duplicate bonas
-- If the second statment return multiple value, it can't be assigned to boass with WHERE claus
SELECT *
FROM Position
WHERE bonas = (SELECT DISTINCT bonas
FROM Position
ORDER BY bonas DESC
LIMIT 1 OFFSET 2)
LIMIT 1;
-- Solution only work for non duplicae bonas
SELECT *
FROM Position
ORDER BY bonas DESC
LIMIT 1 OFFSET 2;
-- Q21, Write a query to display the first record from the Employee table.
SELECT *
FROM Employee
LIMIT 1;
-- Alternating way
SELECT *
FROM Employee
WHERE empID = (SELECT MIN(empID) FROM Employee);
-- Q22, Write a query to display the last record from the Employee table.
SELECT *
FROM Employee
ORDER BY empID DESC
LIMIT 1;
-- Alternating way
SELECT *
FROM Employee
WHERE empID = (SELECT MAX(empID) FROM Employee);
-- Q23 Write a query to retrieve departments who have less than 2 employees working in it.
SELECT department, COUNT(empID) as 'EmpNo'
FROM Employee
GROUP BY department
HAVING COUNT(empID) = 1;
-- Q24, Write a query to retrieve Postion along with total bonas paid for each of them
SELECT position, SUM(bonas)
FROM Position
GROUP BY position;
-- Q25, Write a query to fetch 50% records from the Employee table.
SELECT *
FROM Employee
WHERE empID <= (SELECT COUNT(EmpID)/2
FROM Employee);