-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQL_Test.txt
More file actions
200 lines (166 loc) · 5.74 KB
/
MySQL_Test.txt
File metadata and controls
200 lines (166 loc) · 5.74 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
CREATE DATABASE ORG123;
SHOW DATABASES;
USE ORG123;
show tables from ORG123;
CREATE TABLE Worker (
WORKER_ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
FIRST_NAME CHAR(25),
LAST_NAME CHAR(25),
SALARY INT(15),
JOINING_DATE DATETIME,
DEPARTMENT CHAR(25)
);
INSERT INTO Worker
(WORKER_ID, FIRST_NAME, LAST_NAME, SALARY, JOINING_DATE, DEPARTMENT) VALUES
(001, 'Monika', 'Arora', 100000, '14-02-20 09.00.00', 'HR'),
(002, 'Niharika', 'Verma', 80000, '14-06-11 09.00.00', 'Admin'),
(003, 'Vishal', 'Singhal', 300000, '14-02-20 09.00.00', 'HR'),
(004, 'Amitabh', 'Singh', 500000, '14-02-20 09.00.00', 'Admin'),
(005, 'Vivek', 'Bhati', 500000, '14-06-11 09.00.00', 'Admin'),
(006, 'Vipul', 'Diwan', 200000, '14-06-11 09.00.00', 'Account'),
(007, 'Satish', 'Kumar', 75000, '14-01-20 09.00.00', 'Account'),
(008, 'Geetika', 'Chauhan', 90000, '14-04-11 09.00.00', 'Admin');
CREATE TABLE Bonus (
WORKER_REF_ID INT,
BONUS_AMOUNT INT(10),
BONUS_DATE DATETIME,
FOREIGN KEY (WORKER_REF_ID)
REFERENCES Worker(WORKER_ID)
ON DELETE CASCADE
);
INSERT INTO Bonus
(WORKER_REF_ID, BONUS_AMOUNT, BONUS_DATE) VALUES
(001, 5000, '16-02-20'),
(002, 3000, '16-06-11'),
(003, 4000, '16-02-20'),
(001, 4500, '16-02-20'),
(002, 3500, '16-06-11');
CREATE TABLE Title (
WORKER_REF_ID INT,
WORKER_TITLE CHAR(25),
AFFECTED_FROM DATETIME,
FOREIGN KEY (WORKER_REF_ID)
REFERENCES Worker(WORKER_ID)
ON DELETE CASCADE
);
INSERT INTO Title
(WORKER_REF_ID, WORKER_TITLE, AFFECTED_FROM) VALUES
(001, 'Manager', '2016-02-20 00:00:00'),
(002, 'Executive', '2016-06-11 00:00:00'),
(008, 'Executive', '2016-06-11 00:00:00'),
(005, 'Manager', '2016-06-11 00:00:00'),
(004, 'Asst. Manager', '2016-06-11 00:00:00'),
(007, 'Executive', '2016-06-11 00:00:00'),
(006, 'Lead', '2016-06-11 00:00:00'),
(003, 'Lead', '2016-06-11 00:00:00');
select * from bonus;
select * from title;
select * from worker;
#1. Write an SQL query to fetch unique values of DEPARTMENT from
select distinct DEPARTMENT
from Worker;
#2. Write an SQL query to print all Worker details from the Worker table order by FIRST_NAME Ascending and DEPARTMENT Descending
select *
from Worker
order by FIRST_NAME ASC, DEPARTMENT DESC;
#3. Write an SQL query to print details of the Workers whose FIRST_NAME contains ‘a’
select *
from Worker
where FIRST_NAME LIKE '%a%';
#4. Write an SQL query to print details of the Workers whose FIRST_NAME ends with ‘h’ and contains six alphabets
select *
from Worker
where FIRST_NAME LIKE '%h';
#5. Write an SQL query to print details of the Workers whose SALARY lies between 100000 and 500000
select *
from Worker
where SALARY BETWEEN 100000 AND 500000;
#6. Write an SQL query to print details of the Workers who have joined in Feb’2014.
select *
from Worker
where JOINING_DATE BETWEEN '2014-02-01' AND '2014-02-28';
#7. Write an SQL query to fetch the count of employees working in the department ‘Admin’
select COUNT(*) AS employee_count
from Worker
where DEPARTMENT = 'Admin';
#8. Write an SQL query to fetch worker names with salaries >= 50000 and <= 100000.
select FIRST_NAME, LAST_NAME
from Worker
where SALARY BETWEEN 50000 AND 100000;
#9. Write an SQL query to fetch the no. of workers for each department in the descending order
select DEPARTMENT, COUNT(*) AS number_of_worker
from Worker
group by DEPARTMENT
order by number_of_worker DESC;
#10. Write an SQL query to print details of the Workers who are also Managers
select *
from Worker
where WORKER_ID IN (
select WORKER_REF_ID
from Title
where WORKER_TITLE = 'Manager'
);
#11. Write an SQL query to determine the 2nd lowest salary without using TOP or limit method.
select MIN(SALARY) AS SecondLowestSalary
from Worker
where SALARY > (select MIN(SALARY) from Worker);
#12. Write an SQL query to fetch the list of employees with the same salary
select FIRST_NAME, LAST_NAME, SALARY
from Worker
where SALARY IN (
select SALARY
from Worker
group by SALARY
having COUNT(*) > 1
);
#13. Write an SQL query to show the second highest salary from a table
select MAX(SALARY) AS SecondHighestSalary
from Worker
where SALARY < (select MAX(SALARY) from Worker);
#14. Write an SQL query to show one row twice in results from a table.
select * from Worker
UNION ALL
select * from Worker;
#15. Write an SQL query to fetch the first 50% records from a table.
select *
from Worker
where WORKER_ID <= (
select COUNT(*) / 2
from Worker
);
#16. Write an SQL query to fetch the departments that have less than three people in it.
select DEPARTMENT
from Worker
group by DEPARTMENT
having COUNT(*) < 3;
#17. Write an SQL query to show all departments along with the number of people in there.
select DEPARTMENT, COUNT(*) AS Department_count
from Worker
group by DEPARTMENT;
#18. Write an SQL query to fetch the last five records from a table
select *
from Worker
order by WORKER_ID DESC
limit 5;
#19. Write an SQL query to print the name of employees having the highest salary in each department
select w.FIRST_NAME, w.DEPARTMENT, w.SALARY
from Worker w
where SALARY = (
select MAX(SALARY)
from Worker
where DEPARTMENT = w.DEPARTMENT
);
#20. Write an SQL query to fetch three max salaries from a table
select distinct SALARY
from Worker
order by SALARY DESC
limit 3;
#21. Write an SQL query to print the name of employees having the lowest salary in account and admin department
select FIRST_NAME, DEPARTMENT, SALARY
from Worker w
where DEPARTMENT IN ('Account', 'Admin')
and SALARY = (
select MIN(SALARY)
from Worker
where DEPARTMENT = w.DEPARTMENT
);