-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql2
362 lines (345 loc) · 15.9 KB
/
mysql2
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
1) create database employee_db;
2) create table salary_table(emp_id int primary key,First_name varchar(100),Last_name varchar(100),salary int,joining_date date,department varchar(100));
3) create table reward(employee_ref_id int,date_of_reward date,amount int,foreign key(employee_ref_id)references salary_table(emp_id));
4) insert into salary_table values(1,'Bob','Kinto',100000,'2019-01-20','Fina
nce');
insert into salary_table values(2,'Jerry','Kansxo',600000,'2019-01-15','IT');
insert into salary_table values(3,'Philip','Jose',890000,'2019-02-05','Banking');
insert into salary_table values(4,'John','Abraham',200000,'2019-02-25','Insurance');
insert into salary_table values(5,'Michael','Mathew',220000,'2019-02-28','Finance');
insert into salary_table values(6,'Alex','chreketo',900000,'2019-05-10','IT');
insert into salary_table values(7,'Yohan','Soso',1200000,'2019-06-20','Banking');
1) select * from salary_table;
+--------+------------+-----------+---------+--------------+------------+
| emp_id | First_name | Last_name | salary | joining_date | department |
+--------+------------+-----------+---------+--------------+------------+
| 1 | Bob | Kinto | 100000 | 2019-01-20 | Finance |
| 2 | Jerry | Kansxo | 600000 | 2019-01-15 | IT |
| 3 | Philip | Jose | 890000 | 2019-02-05 | Banking |
| 4 | John | Abraham | 200000 | 2019-02-25 | Insurance |
| 5 | Michael | Mathew | 220000 | 2019-02-28 | Finance |
| 6 | Alex | chreketo | 900000 | 2019-05-10 | IT |
| 7 | Yohan | Soso | 1200000 | 2019-06-20 | Banking |
+--------+------------+-----------+---------+--------------+------------+
insert into reward values(1,'2019-05-11',1000);
insert into reward values(2,'2019-02-15',5000);
insert into reward values(3,'2019-04-22',2000);
insert into reward values(1,'2019-06-20',8000);
select * from reward;
+-----------------+----------------+--------+
| employee_ref_id | date_of_reward | amount |
+-----------------+----------------+--------+
| 1 | 2019-05-11 | 1000 |
| 2 | 2019-02-15 | 5000 |
| 3 | 2019-04-22 | 2000 |
| 1 | 2019-06-20 | 8000 |
+-----------------+----------------+--------+
2) select First_name from salary_table as Employee_name;
+------------+
| First_name |
+------------+
| Bob |
| Jerry |
| Philip |
| John |
| Michael |
| Alex |
| Yohan |
+------------+
3) select lower(Last_name) from salary_table;
+------------------+
| lower(Last_name) |
+------------------+
| kinto |
| kansxo |
| jose |
| abraham |
| mathew |
| chreketo |
| soso |
+------------------+
4) select department from salary_table;
+------------+
| department |
+------------+
| Finance |
| IT |
| Banking |
| Insurance |
| Finance |
| IT |
| Banking |
+------------+
5) select left(First_name,4) as firstfourchar from salary_table;
+---------------+
| firstfourchar |
+---------------+
| Bob |
| Jerr |
| Phil |
| John |
| Mich |
| Alex |
| Yoha |
+---------------+
8) select concat(Last_name,'_',First_name) as full_name from salary_table;
+----------------+
| full_name |
+----------------+
| Kinto_Bob |
| Kansxo_Jerry |
| Jose_Philip |
| Abraham_John |
| Mathew_Michael |
| chreketo_Alex |
| Soso_Yohan |
+----------------+
9) select year(joining_date) as year,month(joining_date) as month,day(joining_date) as day from salary_table;
+------+-------+------+
| year | month | day |
+------+-------+------+
| 2019 | 1 | 20 |
| 2019 | 1 | 15 |
| 2019 | 2 | 5 |
| 2019 | 2 | 25 |
| 2019 | 2 | 28 |
| 2019 | 5 | 10 |
| 2019 | 6 | 20 |
+------+-------+------+
10) select * from salary_table order by First_name;
+--------+------------+-----------+---------+--------------+------------+
| emp_id | First_name | Last_name | salary | joining_date | department |
+--------+------------+-----------+---------+--------------+------------+
| 6 | Alex | chreketo | 900000 | 2019-05-10 | IT |
| 1 | Bob | Kinto | 100000 | 2019-01-20 | Finance |
| 2 | Jerry | Kansxo | 600000 | 2019-01-15 | IT |
| 4 | John | Abraham | 200000 | 2019-02-25 | Insurance |
| 5 | Michael | Mathew | 220000 | 2019-02-28 | Finance |
| 3 | Philip | Jose | 890000 | 2019-02-05 | Banking |
| 7 | Yohan | Soso | 1200000 | 2019-06-20 | Banking |
+--------+------------+-----------+---------+--------------+------------+
11)select * from salary_table order by salary desc;
+--------+------------+-----------+---------+--------------+------------+
| emp_id | First_name | Last_name | salary | joining_date | department |
+--------+------------+-----------+---------+--------------+------------+
| 7 | Yohan | Soso | 1200000 | 2019-06-20 | Banking |
| 6 | Alex | chreketo | 900000 | 2019-05-10 | IT |
| 3 | Philip | Jose | 890000 | 2019-02-05 | Banking |
| 2 | Jerry | Kansxo | 600000 | 2019-01-15 | IT |
| 5 | Michael | Mathew | 220000 | 2019-02-28 | Finance |
| 4 | John | Abraham | 200000 | 2019-02-25 | Insurance |
| 1 | Bob | Kinto | 100000 | 2019-01-20 | Finance |
+--------+------------+-----------+---------+--------------+------------+
12) select * from salary_table where(First_name='Bob' or First_name='Alex');
+--------+------------+-----------+--------+--------------+------------+
| emp_id | First_name | Last_name | salary | joining_date | department |
+--------+------------+-----------+--------+--------------+------------+
| 1 | Bob | Kinto | 100000 | 2019-01-20 | Finance |
| 6 | Alex | chreketo | 900000 | 2019-05-10 | IT |
+--------+------------+-----------+--------+--------------+------------+
13) select * from salary_table where(First_name='Bob');
+--------+------------+-----------+--------+--------------+------------+
| emp_id | First_name | Last_name | salary | joining_date | department |
+--------+------------+-----------+--------+--------------+------------+
| 1 | Bob | Kinto | 100000 | 2019-01-20 | Finance |
+--------+------------+-----------+--------+--------------+------------+
14) select * from salary_table where Last_name like 'k%';
+--------+------------+-----------+--------+--------------+------------+
| emp_id | First_name | Last_name | salary | joining_date | department |
+--------+------------+-----------+--------+--------------+------------+
| 1 | Bob | Kinto | 100000 | 2019-01-20 | Finance |
| 2 | Jerry | Kansxo | 600000 | 2019-01-15 | IT |
+--------+------------+-----------+--------+--------------+------------+
15) select * from salary_table where First_name like '%n';
+--------+------------+-----------+---------+--------------+------------+
| emp_id | First_name | Last_name | salary | joining_date | department |
+--------+------------+-----------+---------+--------------+------------+
| 4 | John | Abraham | 200000 | 2019-02-25 | Insurance |
| 7 | Yohan | Soso | 1200000 | 2019-06-20 | Banking |
+--------+------------+-----------+---------+--------------+------------+
16) select * from salary_table where salary between 200000 and 800000;
+--------+------------+-----------+--------+--------------+------------+
| emp_id | First_name | Last_name | salary | joining_date | department |
+--------+------------+-----------+--------+--------------+------------+
| 2 | Jerry | Kansxo | 600000 | 2019-01-15 | IT |
| 4 | John | Abraham | 200000 | 2019-02-25 | Insurance |
| 5 | Michael | Mathew | 220000 | 2019-02-28 | Finance |
+--------+------------+-----------+--------+--------------+------------+
17) select * from salary_table where joining_date > '2019-03-31';
+--------+------------+-----------+---------+--------------+------------+
| emp_id | First_name | Last_name | salary | joining_date | department |
+--------+------------+-----------+---------+--------------+------------+
| 6 | Alex | chreketo | 900000 | 2019-05-10 | IT |
| 7 | Yohan | Soso | 1200000 | 2019-06-20 | Banking |
+--------+------------+-----------+---------+--------------+------------+
18) select First_name, joining_date from salary_table;
+------------+--------------+
| First_name | joining_date |
+------------+--------------+
| Bob | 2019-01-20 |
| Jerry | 2019-01-15 |
| Philip | 2019-02-05 |
| John | 2019-02-25 |
| Michael | 2019-02-28 |
| Alex | 2019-05-10 |
| Yohan | 2019-06-20 |
+------------+--------------+
19) select salary_table.joining_date as date1,reward.date_of_reward as date2,datediff(reward.date_of_reward,salary_table.joining_date) from salary_table inn
er join reward on salary_table.emp_id=reward.employee_ref_id;
+------------+------------+-----------------------------------------------------------+
| date1 | date2 | datediff(reward.date_of_reward,salary_table.joining_date) |
+------------+------------+-----------------------------------------------------------+
| 2019-01-20 | 2019-05-11 | 111 |
| 2019-01-15 | 2019-02-15 | 31 |
| 2019-02-05 | 2019-04-22 | 76 |
| 2019-01-20 | 2019-06-20 | 151 |
+------------+------------+-----------------------------------------------------------+
20) select now() as current_date_and_time;
+-----------------------+
| current_date_and_time |
+-----------------------+
| 2023-10-08 20:57:36 |
+-----------------------+
21) select department,sum(salary) as total_salary from salary_table group by department;
+------------+--------------+
| department | total_salary |
+------------+--------------+
| Finance | 320000 |
| IT | 1500000 |
| Banking | 2090000 |
| Insurance | 200000 |
+------------+--------------+
22) select department,sum(salary) as total_salary from salary_table group by
department order by sum(salary) desc;
+------------+--------------+
| department | total_salary |
+------------+--------------+
| Banking | 2090000 |
| IT | 1500000 |
| Finance | 320000 |
| Insurance | 200000 |
+------------+--------------+
23) select department,count(emp_id) as no_of_employees,sum(salary) as total_salary from salary_table group by department order by sum(salary) desc;
+------------+-----------------+--------------+
| department | no_of_employees | total_salary |
+------------+-----------------+--------------+
| Banking | 2 | 2090000 |
| IT | 2 | 1500000 |
| Finance | 2 | 320000 |
| Insurance | 1 | 200000 |
+------------+-----------------+--------------+
24) select avg(salary) as average_salary from salary_table gr
oup by department order by avg(salary);
+----------------+
| average_salary |
+----------------+
| 160000.0000 |
| 200000.0000 |
| 750000.0000 |
| 1045000.0000 |
+----------------+
24)) select department,avg(salary) as average_salary from salary_table group by department order by avg(salary);
+------------+----------------+
| department | average_salary |
+------------+----------------+
| Finance | 160000.0000 |
| Insurance | 200000.0000 |
| IT | 750000.0000 |
| Banking | 1045000.0000 |
+------------+----------------+
25) select department,min(salary) from salary_table group by department order by min(salary);
+------------+-------------+
| department | min(salary) |
+------------+-------------+
| Finance | 100000 |
| Insurance | 200000 |
| IT | 600000 |
| Banking | 890000 |
+------------+-------------+
26) select count(emp_id),extract(year_month from joining_date) from salary_table group by extract(year_month from joining_date);
+---------------+---------------------------------------+
| count(emp_id) | extract(year_month from joining_date) |
+---------------+---------------------------------------+
| 2 | 201901 |
| 3 | 201902 |
| 1 | 201905 |
| 1 | 201906 |
+---------------+---------------------------------------+
27) select department,sum(salary) as total_salary from salary_table group by department having total_salary > 100000 order by total_salary desc;
+------------+--------------+
| department | total_salary |
+------------+--------------+
| Banking | 2090000 |
| IT | 1500000 |
| Finance | 320000 |
| Insurance | 200000 |
+------------+--------------+
28) select salary_table.emp_id as emp_id,salary_table.First_name,reward.amount from salary_table inner join reward on salary_table.emp_id=reward.employee_ref_id;
+--------+------------+--------+
| emp_id | First_name | amount |
+--------+------------+--------+
| 1 | Bob | 1000 |
| 2 | Jerry | 5000 |
| 3 | Philip | 2000 |
| 1 | Bob | 8000 |
+--------+------------+--------+
29) select salary,case when First_name='Bob' then salary * 0.20
when first_name = 'alex' then salary * 0.10 else salary * 0.15 end
as calculated_percetage from salary_table;
+---------+----------------------+
| salary | calculated_percetage |
+---------+----------------------+
| 100000 | 20000.00 |
| 600000 | 90000.00 |
| 890000 | 133500.00 |
| 200000 | 30000.00 |
| 220000 | 33000.00 |
| 900000 | 90000.00 |
| 1200000 | 180000.00 |
+---------+----------------------+
30) select case when department = 'IT' then 'IT SERVICES' when department = 'Finanace' then 'Financial services' when department = 'Banking' then 'Banking services' else department end as department from salary_table;
+------------------+
| department |
+------------------+
| Finance |
| IT SERVICES |
| Banking services |
| Insurance |
| Finance |
| IT SERVICES |
| Banking services |
+------------------+
32) select First_name,amount from salary_table join reward on salary_table.emp_id = reward.employee_ref_id where reward.amount is not null;
+------------+--------+
| First_name | amount |
+------------+--------+
| Bob | 1000 |
| Jerry | 5000 |
| Philip | 2000 |
| Bob | 8000 |
+------------+--------+
33) update reward set amount = 1000 where employee_ref_id = (select emp_id from salary_table where First_name = 'Bob');
Query OK, 1 row affected (0.24 sec)
Rows matched: 2 Changed: 1 Warnings: 0
mysql> select * from reward;
+-----------------+----------------+--------+
| employee_ref_id | date_of_reward | amount |
+-----------------+----------------+--------+
| 1 | 2019-05-11 | 1000 |
| 2 | 2019-02-15 | 5000 |
| 3 | 2019-04-22 | 2000 |
| 1 | 2019-06-20 | 1000 |
+-----------------+----------------+--------+
select salary_table.First_name, ifnull(reward.amount,0) as reward_amount from salary_table left join reward on salary_table.emp_id = reward.employee_ref_id;
+------------+---------------+
| First_name | reward_amount |
+------------+---------------+
| Bob | 1000 |
| Bob | 1000 |
| Jerry | 5000 |
| Philip | 2000 |
| John | 0 |
| Michael | 0 |
| Alex | 0 |
| Yohan | 0 |
+------------+---------------+