-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBMS Assignment 3
More file actions
328 lines (289 loc) · 11.7 KB
/
DBMS Assignment 3
File metadata and controls
328 lines (289 loc) · 11.7 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
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
Question No 3
Create a medical database having following table.
1.Patient table (pat_id,pat_name,date_adm,age,city);
2.Doctor table(doc_id,doc_name,qualification,exp,dept,city,salary);
3.Treats table(doc_id,pat_id,disease);foriegn key doc_id,pat_id;
(add on delete cascade constrain)
*Query:--
1. Insert at least 5 records in each table;
2. Display all the patients name between the age group 18-15;
3. Display the list of doctors who are MD;
4. Display the list of doctors whose exp is 20+ years;
5. Display patients suffering from Cancer;
6. Display the patient name and the doctor name who r in cancer;
7. Display the patient name, whose name starts with letter A, End with A, having the exacts 5
letters;
8. Remove all the records of patients with Pat_id =p10;
9. Remove all the records of doctor Suhas;
10. Change the qualification of doctor shubham from mbbs to md;
11. Give five percent salaary raise to the dentist 5% and 10% to cardiologists, in single query;
12. Display dept wise total salary of the doctors;
13. Find the dept, which has the highest avg salary;
14. Find the avg salary of docs in dentist dept;
15. Find the deptwhreravg salary of the instructor is more than 50,000;
16. Find how many docs work in hospital;
17. Find out how many doctors actually treated a patient;
18. List the cities in which either doctors or patient lives;
19. List the cites where both lives;
20. Find the doctors who have not treated any patient;
Query:
Create a medical database having following tables:
Patient table(patient_id,patient_name,Date of Admit,Age,City)
Doctor table(doc_id,doc_name,qualification,experience,dept,city,salary)
Treats table(doc_id,patient_id,disease) (use on delete cascade )
1. Insert at least 5 records in each table.
select * from Patient;
+--------+----------+-------------+------+-----------+
| pat_id | pat_name | DateOfAdmit | age | city |
+--------+----------+-------------+------+-----------+
| a10 | Aryan | 2017-05-11 | 20 | Mumbai |
| c12 | Amit | 2017-07-21 | 39 | Bangalore |
| d13 | Anita | 2017-09-25 | 49 | Pune |
| p10 | Sandesh | 2016-07-21 | 28 | Pune |
| x15 | Suyash | 2017-04-17 | 29 | Delhi |
+--------+----------+-------------+------+-----------+
select * from Doctor;
+--------+----------+---------------+------------+------------+-----------+--------+
| doc_id | doc_name | qualification | experience | dept | city | salary |
+--------+----------+---------------+------------+------------+-----------+--------+
| e1 | Suhas | MD | 10 | Dental | Pune | 70000 |
| r5 | Yogesh | MD | 8 | Dental | Delhi | 40000 |
| s5 | Mangesh | MBBS | 25 | Cardiology | Bangalore | 100000 |
| w8 | Komal | MBBS | 25 | Chemothera | Kolkata | 45000 |
| y3 | Shubham | MBBS | 10 | Cardiology | Mumabi | 60000 |
+--------+----------+---------------+------------+------------+-----------+--------+
select * from Treats;
+--------+--------+--------------+
| doc_id | pat_id | disease |
+--------+--------+--------------+
| w8 | p10 | Cancer |
| w8 | c12 | Cancer |
| e1 | d13 | Toothache |
| s5 | x15 | Heart Attack |
| r5 | a10 | Cavities |
+--------+--------+--------------+
5 rows in set (0.00 sec)
2. Display all the patient names between age group 18 to 50.
select pat_name from Patient where age between 18 and 50;
+----------+
| pat_name |
+----------+
| Aryan |
| Amit |
| Anita |
| Sandesh |
| Suyash |
+----------+
5 rows in set (0.00 sec)
3. Display the list of all doctors who are MD.
select doc_name from Doctor where qualification="MD";
+----------+
| doc_name |
+----------+
| Suhas |
| Yogesh |
+----------+
2 rows in set (0.00 sec)
4. Display the list of all doctors whose experience>20 years.
select doc_name from Doctor where experience>20;
+----------+
| doc_name |
+----------+
| Mangesh |
| Komal |
+----------+
2 rows in set (0.00 sec)
5. Display patient names suffering from cancer.
select pat_name from Patient,Treats where Treats.disease="Cancer" and Treats.pat_id=Patient.pat_id;
+----------+
| pat_name |
+----------+
| Sandesh |
| Amit |
+----------+
2 rows in set (0.02 sec)
6. Display the patient name & doctor name who is treating the cancer patient.
select pat_name,doc_name from Patient as p,Doctor as d,Treats as t where disease="Cancer" and d.doc_id=t.doc_id and p.pat_id=t.pat_id;
+----------+----------+
| pat_name | doc_name |
+----------+----------+
| Sandesh | Komal |
| Amit | Komal |
+----------+----------+
2 rows in set (0.00 sec)
7. Display the patient names whose name starts with letter 'a',end with 'a',having a name having exactly 5 letters.
select pat_name from Patient where pat_name like "a%";
+----------+
| pat_name |
+----------+
| Aryan |
| Amit |
| Anita |
+----------+
3 rows in set (0.00 sec)
mysql> select pat_name from Patient where pat_name like "%a";
+----------+
| pat_name |
+----------+
| Anita |
+----------+
1 row in set (0.00 sec)
mysql> select pat_name from Patient where pat_name like "_ _ _ _ _";
+----------+
| pat_name |
+----------+
| Aryan |
| Anita |
+----------+
2 rows in set (0.00 sec)
8. Remove all the records of patient with patient_id=p10.
delete from Patient where pat_id="p10";
Query OK, 1 row affected (0.04 sec)
mysql> select * from Patient;
+--------+----------+-------------+------+-----------+
| pat_id | pat_name | DateOfAdmit | age | city |
+--------+----------+-------------+------+-----------+
| a10 | Aryan | 2017-05-11 | 20 | Mumbai |
| c12 | Amit | 2017-07-21 | 39 | Bangalore |
| d13 | Anita | 2017-09-25 | 49 | Pune |
| x15 | Suyash | 2017-04-17 | 29 | Delhi |
+--------+----------+-------------+------+-----------+
4 rows in set (0.00 sec)
select * from Treats;
+--------+--------+--------------+
| doc_id | pat_id | disease |
+--------+--------+--------------+
| w8 | c12 | Cancer |
| e1 | d13 | Toothache |
| s5 | x15 | Heart Attack |
| r5 | a10 | Cavities |
+--------+--------+--------------+
4 rows in set (0.00 sec)
9. Remove all the records of doctor Suhas.
select * from Treats;
+--------+--------+--------------+
| doc_id | pat_id | disease |
+--------+--------+--------------+
| w8 | c12 | Cancer |
| s5 | x15 | Heart Attack |
| r5 | a10 | Cavities |
+--------+--------+--------------+
3 rows in set (0.00 sec)
mysql> select * from Doctor;
+--------+----------+---------------+------------+------------+-----------+--------+
| doc_id | doc_name | qualification | experience | dept | city | salary |
+--------+----------+---------------+------------+------------+-----------+--------+
| r5 | Yogesh | MD | 8 | Dental | Delhi | 40000 |
| s5 | Mangesh | MBBS | 25 | Cardiology | Bangalore | 100000 |
| w8 | Komal | MBBS | 25 | Chemothera | Kolkata | 45000 |
| y3 | Shubham | MBBS | 10 | Cardiology | Mumabi | 60000 |
+--------+----------+---------------+------------+------------+-----------+--------+
4 rows in set (0.00 sec)
10. Change the qualification of doctor Shubham from MBBS to MD.
update Doctor set qualification="MD" where doc_name="Shubham";
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from Doctor;
+--------+----------+---------------+------------+------------+-----------+--------+
| doc_id | doc_name | qualification | experience | dept | city | salary |
+--------+----------+---------------+------------+------------+-----------+--------+
| r5 | Yogesh | MD | 8 | Dental | Delhi | 40000 |
| s5 | Mangesh | MBBS | 25 | Cardiology | Bangalore | 100000 |
| w8 | Komal | MBBS | 25 | Chemothera | Kolkata | 45000 |
| y3 | Shubham | MD | 10 | Cardiology | Mumabi | 60000 |
+--------+----------+---------------+------------+------------+-----------+--------+
4 rows in set (0.00 sec)
11. Give 5% salary rates to the dentist and 10% raise to cardiologist (in single query).
update Doctor set salary=case when dept="Dental" then salary+salary*(0.05) when dept="Cardiology" then salary+salary*(0.1) else salary*1 end;
Query OK, 3 rows affected (0.05 sec)
Rows matched: 4 Changed: 3 Warnings: 0
mysql> select * from Doctor;
+--------+----------+---------------+------------+------------+-----------+--------+
| doc_id | doc_name | qualification | experience | dept | city | salary |
+--------+----------+---------------+------------+------------+-----------+--------+
| r5 | Yogesh | MD | 8 | Dental | Delhi | 42000 |
| s5 | Mangesh | MBBS | 25 | Cardiology | Bangalore | 110000 |
| w8 | Komal | MBBS | 25 | Chemothera | Kolkata | 45000 |
| y3 | Shubham | MD | 10 | Cardiology | Mumabi | 66000 |
+--------+----------+---------------+------------+------------+-----------+--------+
4 rows in set (0.00 sec)
12. Display dept wise total salary of doctors.
select dept,sum(salary) from Doctor group by dept;
+------------+-------------+
| dept | sum(salary) |
+------------+-------------+
| Cardiology | 176000 |
| Chemothera | 45000 |
| Dental | 42000 |
+------------+-------------+
3 rows in set (0.00 sec)
13. Find the dept that have the highest avg salary.
select dept,avg(salary) from Doctor group by dept having avg(salary)>=all(select avg(salary) from Doctor group by dept);
+------------+-------------+
| dept | avg(salary) |
+------------+-------------+
| Cardiology | 88000.0000 |
+------------+-------------+
1 row in set (0.00 sec)
14. Find the avg salary of the doctors in dentist dept.
select avg(salary) from Doctor where dept="Dental";
+-------------+
| avg(salary) |
+-------------+
| 42000.0000 |
+-------------+
1 row in set (0.00 sec)
15. Find the dept where avg salary of the instructor is more than 50,000.
select dept from Doctor group by dept having avg(salary)>50000;
+------------+
| dept |
+------------+
| Cardiology |
+------------+
1 row in set (0.02 sec)
16. Find how many doctors work in hospital.
select count(*) from Doctor;
+----------+
| count(*) |
+----------+
| 4 |
+----------+
1 row in set (0.00 sec)
17. Find out how many doctors actually treated a patient.
select count(distinct doc_id) from Treats;
+------------------------+
| count(distinct doc_id) |
+------------------------+
| 3 |
+------------------------+
1 row in set (0.00 sec)
18. List the cities in which either doctor or patient lives.
select city from Doctor union select city from Patient;
+-----------+
| city |
+-----------+
| Delhi |
| Bangalore |
| Kolkata |
| Mumbai |
| Pune |
+-----------+
5 rows in set (0.01 sec)
19. List the cities in which both the patient & the doctor lives.
select d.city from Doctor as d,Patient as p where d.city=p.city;
+-----------+
| city |
+-----------+
| Mumbai |
| Bangalore |
| Delhi |
+-----------+
3 rows in set (0.00 sec)
20. Find out the doctors who have not treated any patient.
select doc_name,doc_id from Doctor where doc_id not in(select distinct doc_id from Treats);
+----------+--------+
| doc_name | doc_id |
+----------+--------+
| Shubham | y3 |
+----------+--------+
1 row in set (0.00 sec)