@@ -18,31 +18,20 @@ Show patient_id and first_name from patients where their first_name start and en
18
18
at least 6 characters long.
19
19
20
20
select patient_id, first_name from patients where first_name like ' s%s' and len(first_name)>=6
21
-
22
-
23
21
------------------------------------------------------------------------------------------------------------
24
-
25
22
Show patient_id, first_name, last_name from patients whos diagnosis is ' Dementia' .
26
23
27
24
Primary diagnosis is stored in the admissions table.
28
25
29
26
select p.patient_id, first_name, last_name from patients p join admissions a on p.patient_id = a.patient_id
30
27
where diagnosis = ' Dementia'
31
28
32
-
33
-
34
-
35
29
------------------------------------------------------------------------------------------------------------
36
-
37
30
Display every patient' s first_name.
38
31
Order the list by the length of each name and then by alphbetically
39
32
40
33
select first_name from patients p order by len(first_name) , first_name
41
-
42
-
43
-
44
34
-- ----------------------------------------------------------------------------------------------------------
45
-
46
35
Show the total amount of male patients and the total amount of female patients in the patients table.
47
36
Display the two results in the same row.
48
37
@@ -51,12 +40,6 @@ select
51
40
sum (case when gender = ' F' then 1 end) as female_count
52
41
from patients;
53
42
54
-
55
-
56
-
57
-
58
-
59
-
60
43
-- ----------------------------------------------------------------------------------------------------------
61
44
Show first and last name, allergies from patients which have allergies to either ' Penicillin' or ' Morphine' . Show results
62
45
ordered ascending by allergies then by first_name then by last_name.
@@ -66,182 +49,133 @@ select first_name,last_name,allergies
66
49
from patients where allergies in (' Penicillin' , ' Morphine' )
67
50
68
51
order by allergies, first_name,last_name
69
-
70
-
71
-
72
-
73
-
74
52
-- ----------------------------------------------------------------------------------------------------------
75
-
76
-
77
53
Show patient_id, diagnosis from admissions. Find patients admitted multiple times for the same diagnosis.
78
54
79
-
80
-
81
55
select
82
56
patient_id, diagnosis from admissions
83
57
group by diagnosis,patient_id
84
58
having count (* )> 1
85
59
86
-
87
-
88
-
89
-
90
60
-- ----------------------------------------------------------------------------------------------------------
91
-
92
61
Show the city and the total number of patients in the city.
93
62
Order from most to least patients and then by city name ascending.
94
63
95
-
96
64
select
97
65
98
66
city, count (patient_id) as x from patients
99
67
group by city
100
68
order by x desc , city
101
69
102
-
103
-
104
-
105
-
106
-
107
70
-- ----------------------------------------------------------------------------------------------------------
108
-
109
-
110
71
Show first name, last name and role of every person that is either patient or doctor.
111
72
The roles are either " Patient" or " Doctor"
112
73
113
-
114
74
select
115
75
116
76
first_name,last_name,' Patient' from patients
117
77
union all
118
78
119
79
select
120
-
121
80
first_name,last_name,' Doctor' from doctors
122
81
123
-
124
-
125
-
126
- -- ----------------------------------------------------------------------------------------------------------
127
-
82
+ -- ----------------------------------------------------------------------------------------------------------
128
83
Show all allergies ordered by popularity. Remove NULL values from query.
129
84
130
-
131
-
132
-
133
85
select
134
86
allergies, count (* ) as total_diagnosis
135
87
from patients where allergies is not null group by allergies order by total_diagnosis desc
136
88
137
-
138
-
139
-
140
-
141
-
142
-
143
89
-- ----------------------------------------------------------------------------------------------------------
144
90
91
+ Show all patient' s first_name, last_name, and birth_date who were born in the 1970s decade. Sort the
92
+ list starting from the earliest birth_date.
145
93
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
- -- ----------------------------------------------------------------------------------------------------------
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
94
+ select first_name,last_name, birth_date from patients
95
+ where year(birth_date) between 1970 and 1979
96
+ order by birth_date
165
97
166
98
167
99
------------------------------------------------------------------------------------------------------------
168
100
101
+ We want to display each patient' s full name in a single column. Their last_name in all upper
102
+ letters must appear first, then first_name in all lower case letters. Separate the last_name
103
+ and first_name with a comma. Order the list by the first_name in decending order
104
+ EX: SMITH,jane
169
105
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
106
+ select
107
+ concat(upper (last_name), ' ,' , lower (first_name)) as new_name_format from patients
108
+ order by first_name desc
179
109
180
110
-- ----------------------------------------------------------------------------------------------------------
111
+ Show the province_id(s), sum of height; where the total sum of its patient' s height is greater than or equal to 7,000.
181
112
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
113
+ select
114
+ province_id, sum(height)
115
+ from patients group by province_id
116
+ having sum(height) >=7000
192
117
------------------------------------------------------------------------------------------------------------
118
+ Show the difference between the largest weight and smallest weight for patients with the last name ' Maroni'
193
119
194
120
195
-
196
-
197
-
198
-
199
-
200
-
201
-
202
-
203
-
204
-
121
+ select
122
+ max(weight) - min(weight)
123
+ from patients where last_name = ' Maroni'
205
124
206
125
------------------------------------------------------------------------------------------------------------
126
+ Show all of the days of the month (1-31) and how many
127
+ admission_dates occurred on that day. Sort by the day with
128
+ most admissions to least admissions.
207
129
130
+ select
208
131
132
+ day(admission_date) as day_number,
133
+ count(admission_date) as number_of_admissions
209
134
135
+ from admissions
210
136
211
-
212
-
213
-
214
-
215
-
216
-
137
+ group by day(admission_date)
138
+ order by number_of_admissions desc
217
139
------------------------------------------------------------------------------------------------------------
140
+ Show all columns for patient_id 542' s most recent admission_date.
218
141
142
+ select * from admissions a where a .patient_id = 542 order by admission_date desc limit 1
219
143
220
-
221
-
222
-
223
-
224
-
225
-
226
-
227
-
144
+ SELECT *
145
+ FROM admissions
146
+ GROUP BY patient_id
147
+ HAVING
148
+ patient_id = 542
149
+ AND max (admission_date)
228
150
229
151
-- ----------------------------------------------------------------------------------------------------------
152
+ Show patient_id, attending_doctor_id, and diagnosis for admissions that match one of the two criteria:
153
+ 1 . patient_id is an odd number and attending_doctor_id is either 1 , 5 , or 19 .
154
+ 2 . attending_doctor_id contains a 2 and the length of patient_id is 3 characters.
230
155
231
156
157
+ select
232
158
159
+ patient_id, attending_doctor_id, diagnosis
233
160
161
+ from admissions
234
162
163
+ where mod(patient_id,2 ) = 1 and attending_doctor_id in (1 ,5 ,19 )
235
164
236
-
237
-
165
+ or attending_doctor_id like ' %2%' and len(patient_id) = 3
238
166
239
167
-- ----------------------------------------------------------------------------------------------------------
240
168
169
+ Show first_name, last_name, and the total number of admissions attended for each doctor.
241
170
171
+ Every admission has been attended by a doctor.
242
172
173
+ select
243
174
175
+ first_name, last_name, count (* )
176
+ from doctors d join admissions a on d .doctor_id = a .attending_doctor_id
244
177
178
+ group by first_name,last_name
245
179
246
180
247
181
@@ -250,44 +184,51 @@ allergies, count(*) as total_diagnosis
250
184
-- ----------------------------------------------------------------------------------------------------------
251
185
252
186
187
+ For each doctor, display their id, full name, and the first and last admission date they attended.
253
188
189
+ select
254
190
191
+ d .doctor_id , concat(first_name, ' ' ,last_name) as full_name, min (admission_date) first_admission_date, max (admission_date) last_admission_date
192
+ from doctors d join admissions a on d .doctor_id = a .attending_doctor_id
255
193
256
-
257
-
258
-
194
+ group by d .doctor_id , first_name,last_name
259
195
260
196
261
197
-- ----------------------------------------------------------------------------------------------------------
262
198
199
+ Display the total amount of patients for each province. Order by descending.
263
200
201
+ select
264
202
203
+ p2 .province_name , count (p .patient_id ) as x
204
+ from patients p join province_names p2 on p .province_id = p2 .province_id
205
+ group by p2 .province_name
265
206
266
-
267
-
268
-
207
+ order by x desc
269
208
270
209
271
210
-- ----------------------------------------------------------------------------------------------------------
211
+ For every admission, display the patient' s full name, their admission
212
+ diagnosis, and their doctor' s full name who diagnosed their problem.
272
213
214
+ select
273
215
274
-
275
-
276
-
277
-
278
-
279
-
216
+ concat(p .first_name , ' ' ,p .last_name ) as patient_name, diagnosis,
217
+ concat(d .first_name , ' ' , d .last_name ) as doctor_name
218
+ from admissions a join patients p on p .patient_id = a .patient_id join doctors d
219
+ on d .doctor_id = a .attending_doctor_id
280
220
281
221
282
222
-- ----------------------------------------------------------------------------------------------------------
283
223
284
224
225
+ display the number of duplicate patients based on their first_name and last_name.
226
+ select
227
+ first_name, last_name, count (* ) as num_of_duplicates
228
+ from patients
285
229
230
+ group by first_name,last_name
231
+ having count (* ) > 1
286
232
287
-
288
-
289
-
290
-
291
-
292
- -- ----------------------------------------------------------------------------------------------------------
233
+ order by num_of_duplicates desc
293
234
0 commit comments