-
Notifications
You must be signed in to change notification settings - Fork 0
/
assignment1.sql
311 lines (271 loc) · 9.34 KB
/
assignment1.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
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
CREATE DATABASE ORG1;
SHOW DATABASES;
USE ORG1;
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 FIRST_NAME as Worker_name from Worker;
select Upper(FIRST_NAME) as Upper_Case_FIRST_NAME FROM Worker;
SELECT DISTINCT Department FROM Worker ORDER BY Department;
SELECT SUBSTRING(first_name, 1, 3) FROM worker;
SELECT POSITION('A' IN 'AMITABH') AS POSITION;
SELECT RTRIM(FIRST_NAME) FROM Worker;
SELECT LTRIM(DEPARTMENT) AS trimmed_department
FROM Worker;
SELECT DISTINCT DEPARTMENT, LENGTH(DEPARTMENT) AS department_length
FROM Worker;
SELECT REPLACE(FIRST_NAME, 'a', 'A') AS updated_first_name
FROM Worker;
SELECT CONCAT(FIRST_NAME, ' ', LAST_NAME) AS COMPLETE_NAME
FROM Worker;
SELECT * FROM Worker ORDER BY FIRST_NAME ASC;
SELECT * FROM Worker ORDER BY FIRST_NAME ASC, DEPARTMENT DESC;
SELECT * FROM Worker where FIRST_NAME in('Vipul','Satish');
SELECT * FROM Worker where FIRST_NAME not in('Vipul','Satish');
SELECT * FROM Worker where DEPARTMENT='Admin' ;
SELECT * FROM Worker where FIRST_NAME LIKE '%a%';
SELECT * FROM Worker where FIRST_NAME LIKE '%a';
SELECT * FROM Worker where FIRST_NAME LIKE '%h' AND LENGTH(FIRST_NAME)=6;
SELECT * FROM Worker where SALARY between 100000 and 500000;
SELECT * FROM Worker where JOINING_DATE between '2014-01-31' and '2014-03-01';
SELECT COUNT(WORKER_ID) AS COUNT FROM Worker WHERE DEPARTMENT='Admin';
SELECT FIRST_NAME,LAST_NAME FROM Worker where SALARY>=50000 and SALARY<=100000;
SELECT COUNT(WORKER_ID) AS COUNT FROM Worker GROUP BY DEPARTMENT ORDER BY COUNT(WORKER_ID) DESC;
SELECT w.WORKER_ID, w.FIRST_NAME, w.LAST_NAME, w.SALARY, w.JOINING_DATE, w.DEPARTMENT
FROM Worker w
INNER JOIN Title t ON w.WORKER_ID = t.WORKER_REF_ID
WHERE t.WORKER_TITLE = 'Manager';
SELECT FIRST_NAME, LAST_NAME, COUNT(*) FROM Worker GROUP BY FIRST_NAME, LAST_NAME HAVING COUNT(*) > 1;
CREATE DATABASE ORG1;
SHOW DATABASES;
USE ORG1;
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 FIRST_NAME as Worker_name from Worker;
select Upper(FIRST_NAME) as Upper_Case_FIRST_NAME FROM Worker;
SELECT DISTINCT Department FROM Worker ORDER BY Department;
SELECT SUBSTRING(first_name, 1, 3) FROM worker;
SELECT POSITION('A' IN 'AMITABH') AS POSITION;
SELECT RTRIM(FIRST_NAME) FROM Worker;
SELECT LTRIM(DEPARTMENT) AS trimmed_department
FROM Worker;
SELECT DISTINCT DEPARTMENT, LENGTH(DEPARTMENT) AS department_length
FROM Worker;
SELECT REPLACE(FIRST_NAME, 'a', 'A') AS updated_first_name
FROM Worker;
SELECT CONCAT(FIRST_NAME, ' ', LAST_NAME) AS COMPLETE_NAME
FROM Worker;
SELECT * FROM Worker ORDER BY FIRST_NAME ASC;
SELECT * FROM Worker ORDER BY FIRST_NAME ASC, DEPARTMENT DESC;
SELECT * FROM Worker where FIRST_NAME in('Vipul','Satish');
SELECT * FROM Worker where FIRST_NAME not in('Vipul','Satish');
SELECT * FROM Worker where DEPARTMENT='Admin' ;
SELECT * FROM Worker where FIRST_NAME LIKE '%a%';
SELECT * FROM Worker where FIRST_NAME LIKE '%a';
SELECT * FROM Worker where FIRST_NAME LIKE '%h' AND LENGTH(FIRST_NAME)=6;
SELECT * FROM Worker where SALARY between 100000 and 500000;
SELECT * FROM Worker where JOINING_DATE between '2014-01-31' and '2014-03-01';
SELECT COUNT(WORKER_ID) AS COUNT FROM Worker WHERE DEPARTMENT='Admin';
SELECT FIRST_NAME,LAST_NAME FROM Worker where SALARY>=50000 and SALARY<=100000;
SELECT COUNT(WORKER_ID) AS COUNT FROM Worker GROUP BY DEPARTMENT ORDER BY COUNT(WORKER_ID) DESC;
SELECT w.WORKER_ID, w.FIRST_NAME, w.LAST_NAME, w.SALARY, w.JOINING_DATE, w.DEPARTMENT
FROM Worker w
INNER JOIN Title t ON w.WORKER_ID = t.WORKER_REF_ID
WHERE t.WORKER_TITLE = 'Manager';
SELECT FIRST_NAME, LAST_NAME, COUNT(*) FROM Worker GROUP BY FIRST_NAME, LAST_NAME HAVING COUNT(*) > 1;
SELECT *
FROM (
SELECT *, ROW_NUMBER() OVER (ORDER BY WORKER_ID) AS row_num
FROM Worker
) AS temp_table
WHERE row_num % 2 != 0;
SELECT *
FROM (
SELECT *, ROW_NUMBER() OVER (ORDER BY WORKER_ID) AS row_num
FROM Worker
) AS temp_table
WHERE row_num % 2 = 0;
CREATE TABLE Worker_Clone AS
SELECT *
FROM Worker;
SELECT w.WORKER_ID, w.FIRST_NAME, w.LAST_NAME, b.BONUS_AMOUNT, b.BONUS_DATE
FROM Worker w
INNER JOIN Bonus b
ON w.WORKER_ID = b.WORKER_REF_ID;
SELECT w.WORKER_ID, w.FIRST_NAME, w.LAST_NAME, w.SALARY, w.JOINING_DATE, w.DEPARTMENT
FROM Worker w
LEFT JOIN Bonus b
ON w.WORKER_ID = b.WORKER_REF_ID
WHERE b.WORKER_REF_ID IS NULL;
SELECT NOW() AS current_datetime;
SELECT *
FROM Worker
ORDER BY WORKER_ID
LIMIT 10;
SELECT DISTINCT SALARY
FROM Worker
ORDER BY SALARY DESC
LIMIT 1 OFFSET 4;
SELECT DISTINCT SALARY
FROM Worker
WHERE 4 = (
SELECT COUNT(DISTINCT SALARY)
FROM Worker
WHERE SALARY > w.SALARY
);
SELECT w1.FIRST_NAME, w1.LAST_NAME, w1.SALARY
FROM Worker w1
INNER JOIN Worker w2 ON w1.SALARY = w2.SALARY
GROUP BY w1.WORKER_ID
HAVING COUNT(w1.WORKER_ID) > 1;
SELECT MAX(SALARY) AS SecondHighestSalary
FROM Worker
WHERE SALARY < (
SELECT MAX(SALARY)
FROM Worker
);
SELECT *
FROM Worker
WHERE WORKER_ID = 1
UNION
SELECT *
FROM Worker
WHERE WORKER_ID = 1;
SELECT w.WORKER_ID, w.FIRST_NAME, w.LAST_NAME, b.BONUS_AMOUNT, b.BONUS_DATE
FROM Worker w
INNER JOIN Bonus b
ON w.WORKER_ID = b.WORKER_REF_ID;
SELECT *
FROM (
SELECT *, ROW_NUMBER() OVER (ORDER BY WORKER_ID) AS row_num
FROM Worker
) AS temp_table
WHERE row_num <= (SELECT COUNT(*) FROM Worker) / 2;
SELECT DEPARTMENT, COUNT(*) AS num_people
FROM Worker
GROUP BY DEPARTMENT
HAVING COUNT(*) < 5;
SELECT DEPARTMENT, COUNT(*) AS num_people
FROM Worker
GROUP BY DEPARTMENT;
SELECT *
FROM Worker
ORDER BY WORKER_ID DESC
LIMIT 1;
SELECT *
FROM Worker
LIMIT 1;
SELECT *
FROM Worker
ORDER BY WORKER_ID DESC
LIMIT 5;
SELECT FIRST_NAME, LAST_NAME, SALARY, DEPARTMENT
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY DEPARTMENT ORDER BY SALARY DESC) AS rn
FROM Worker
) AS temp_table
WHERE rn = 1;
SELECT DISTINCT SALARY
FROM Worker
ORDER BY SALARY DESC
LIMIT 3;
SELECT DISTINCT SALARY
FROM Worker
ORDER BY SALARY ASC
LIMIT 3;
SELECT DISTINCT SALARY
FROM Worker
ORDER BY SALARY DESC
LIMIT 1 OFFSET 4;
SELECT DEPARTMENT, SUM(SALARY) AS total_salary
FROM Worker
GROUP BY DEPARTMENT;
SELECT FIRST_NAME, LAST_NAME
FROM Worker
WHERE SALARY = (
SELECT MAX(SALARY)
FROM Worker
);