-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path036.sql
More file actions
43 lines (36 loc) · 1.02 KB
/
036.sql
File metadata and controls
43 lines (36 loc) · 1.02 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
/* Q: For each doctor, display their id, full name, and the first and last admission date they attended.
Table Info: admissions
+----------------------+----------+
| Column Name | Type |
+----------------------+----------+
| patient_id | INT |
| admission_date | DATE |
| discharge_date | DATE |
| diagnosis | TEXT |
| attending_doctor_id | INT |
+----------------------+----------+
Table Info: doctors
+------------+----------+
| Column | Type |
+------------+----------+
| doctor_id | INT |
| first_name | TEXT |
| last_name | TEXT |
| specialty | TEXT |
+------------+----------+
*/
-- SOLUTION:
select
d.doctor_id,
concat(d.first_name, ' ', d.last_name) as full_name,
min(a.admission_date) as first_admission_date,
max(a.admission_date) as last_admission_date
from doctors d
join admissions a
on d.doctor_id = a.attending_doctor_id
group by
d.doctor_id,
d.first_name,
d.last_name
order by
d.doctor_id;