-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScript 11.sql
More file actions
253 lines (195 loc) · 6.27 KB
/
Copy pathScript 11.sql
File metadata and controls
253 lines (195 loc) · 6.27 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
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
create database org;
use org;
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 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-20 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 values
(001,5000,'16-02-20'),
(002,3000,'16-06-11'),
(003,4000,'16-02-20'),
(001,4500,'16-02-20'),
(002,3000,'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 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'),
(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');
-- que 1
select first_name as worker_name from worker;
-- 2
select upper(first_name) from worker;
-- 3
select distinct department from worker;
select department from worker group by department;
-- 4
select substring(first_name,1,3) from worker;
-- 5
select instr(first_name,'b') from worker where first_name = 'amitabh';
-- 6
select rtrim(first_name) from worker;
-- 7
select ltrim(department) from worker;
-- 8
select distinct department,length(department) from worker;
-- 9
select replace(first_name,'a','A') from worker;
-- 10
select concat(first_name, ' ', last_name) as complete_name from worker;
-- 11
select * from worker order by first_name ASC;
-- 12
select * from worker order by first_name ASC, department DESC;
-- 13
select * from worker where first_name = 'vipul' or first_name = 'satish';
select * from worker where first_name in ('vipul','satish');
-- 14
select * from worker where first_name not in ('vipul','satish');
-- 15
select * from worker where department like 'admin%';
-- 16
select * from worker where first_name like '%a%';
-- 17
select * from worker where first_name like '%a';
-- 18
select * from worker where first_name like '%h' and length(first_name) = 6;
select * from worker where first_name like '_____h';
-- 19
select * from worker where salary between 100000 and 500000;
-- 20
select * from worker where year(joining_date) = 2014 and
month(joining_date) = 02;
-- 21
select department, count(*) from worker where department = 'Admin';
-- 22
select concat(first_name,' ',last_name) as fullname, salary from worker
where salary between 50000 and 100000;
-- 23
select department, count(worker_id) from worker group by department
order by count(worker_id) DESC;
-- 24
select w.* from worker as w inner join title as t
on w.worker_id = t.worker_ref_id
where t.worker_title = 'manager';
-- 25
select worker_title,count(*) from title group by worker_title
having count(*) > 1;
-- 26
select * from worker where mod(worker_id,2) != 0;
-- 27
select * from worker where mod(worker_id,2) = 0;
-- 28
create table worker_clone like worker;
insert into worker_clone select * from worker;
-- 29
select worker.* from worker inner join worker_clone using(worker_id);
-- 30
select * from worker left join worker_clone using(worker_id)
where worker_clone.worker_id is null;
-- 31
select current_timestamp();
-- 32
select * from worker order by salary DESC limit 5;
-- 33
select * from worker order by salary DESC limit 4,1;
-- 34
select salary from worker w1 where 5 = (
select count(distinct w2.salary) from worker w2
where w2.salary >= w1.salary
);
-- 35
select * from worker w1,worker w2 where w1.salary = w2.salary
and w1.worker_id != w2.worker_id;
-- 36
select max(salary) from worker
where salary not in (select max(salary) from worker);
-- 37
select * from worker
union all
select * from worker order by worker_id;
-- 38
select worker_id from worker where worker_id not in
(select worker_ref_id from bonus);
-- 39 works only if auto increament
select * from worker where worker_id <=
(select count(*)/2 from worker);
-- 40
select department from worker group by department
having count(department) < 4;
-- 41
select department,count(*) as dept_count from worker
group by department;
-- 42
select * from worker where worker_id in
(select max(worker_id) from worker);
-- 43
select * from worker where worker_id in
(select min(worker_id) from worker);
-- 44
(select * from worker order by worker_id DESC limit 5)
order by worker_id ASC;
-- 45
select w.department,w.first_name,w.salary from
(select max(salary) as maxsal,department from worker
group by department) as temp
inner join
worker as w on temp.department = w.department and
temp.maxsal = w.salary;
-- 46
select distinct salary from worker w1 where 3 >= (
select count(distinct salary) from worker w2
where w1.salary <= w2.salary
) order by w1.salary DESC;
select distinct salary from worker order by salary DESC limit 3;
-- 47
select distinct salary from worker w1 where 3 >= (
select count(distinct salary) from worker w2
where w1.salary >= w2.salary
) order by w1.salary DESC;
select distinct salary from worker order by salary ASC limit 3;
-- 48
select distinct salary from worker w1 where n >= (
select count(distinct salary) from worker w2
where w1.salary <= w2.salary
) order by w1.salary DESC;
select distinct salary from worker order by salary DESC limit n;
-- 49
select department, sum(salary) from worker group by department;
-- 50
select first_name from worker where salary = (
select max(salary) from worker
);