-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmedium-questions.sql
More file actions
369 lines (337 loc) · 9.68 KB
/
medium-questions.sql
File metadata and controls
369 lines (337 loc) · 9.68 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
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
363
364
365
366
367
368
369
-- Show unique birth years FROM patients and order them by ascending.
SELECT
DISTINCT YEAR(birth_date) AS birth_year
FROM patients
ORDER BY birth_year;
/*
Show unique first names FROM the patients table which only occurs once in the list.
For example, if two or more people are named 'John' in the first_name column then don't include their name in the output list.
If only 1 person is named 'Leo' then include them in the output.
*/
SELECT first_name
FROM patients
GROUP BY first_name
HAVING COUNT(first_name) = 1;
/*
Show patient_id and first_name FROM patients where their first_name start and ends with 's' and is at least 6 characters long.
*/
SELECT
patient_id,
first_name
FROM patients
WHERE
first_name LIKE 's%s'
AND LEN(first_name) > 5;
/*
Show patient_id, first_name, last_name FROM patients whos diagnosis is 'Dementia'.
Primary diagnosis is stored in the admissions table.
*/
SELECT
patients.patient_id,
first_name,
last_name
FROM patients
JOIN admissions ON patients.patient_id = admissions.patient_id
WHERE diagnosis = 'Dementia';
/*
Display every patient's first_name.
Order the list by the length of each name and then by alphabetically.
*/
SELECT first_name
FROM patients
ORDER BY
LEN(first_name),
first_name;
-- Show first name, last name, and gender of patients whose gender is 'M'
SELECT
first_name,
last_name,
gender
FROM patients
WHERE gender = 'M';
/*
Show the total amount of male patients and the total amount of female patients in the patients table.
Display the two results in the same row.
*/
SELECT
SUM(Gender = 'M') AS male_count,
SUM(Gender = 'F') AS female_count
FROM patients;
/*
Show first and last name, allergies FROM patients which have allergies to either 'Penicillin' or 'Morphine'.
Show results ordered ascending by allergies then by first_name then by last_name.
*/
SELECT
first_name,
last_name,
allergies
FROM patients
WHERE
allergies = 'Penicillin'
OR allergies = 'Morphine'
ORDER BY
allergies,
first_name,
last_name;
-- Show patient_id, diagnosis FROM admissions. Find patients admitted multiple times for the same diagnosis.
SELECT
patient_id,
diagnosis
FROM admissions
GROUP BY
patient_id,
diagnosis
HAVING COUNT(*) > 1;
/*
Show the city and the total number of patients in the city.
Order FROM most to least patients and then by city name ascending.
*/
SELECT
city,
COUNT(*) AS num_patients
FROM patients
GROUP BY city
ORDER BY
num_patients DESC,
city ASC;
/*
Show first name, last name and role of every person that is either patient or doctor.
The roles are either "Patient" or "Doctor"
*/
SELECT
first_name,
last_name,
'Patient' AS role
FROM patients
UNION ALL
SELECT
first_name,
last_name,
'Doctor' AS role
FROM doctors;
/*
Show all allergies ordered by popularity. Remove NULL values FROM query.
*/
SELECT
allergies,
COUNT(*) AS total_diagnosis
FROM patients
WHERE allergies IS NOT NULL
GROUP BY allergies
ORDER BY total_diagnosis DESC;
/*
Show all patient's first_name, last_name, and birth_date who were born in the 1970s decade.
Sort the list starting FROM the earliest birth_date.
*/
SELECT
first_name,
last_name,
birth_date
FROM patients
WHERE
YEAR(birth_date) BETWEEN 1970 AND 1979
ORDER BY birth_date ASC;
/*
We want to display each patient's full name in a single column.
Their last_name in all upper letters must appear first, then first_name in all lower case letters.
Separate the last_name and first_name with a comma. Order the list by the first_name in decending order
EX: SMITH,jane
*/
SELECT
CONCAT(UPPER(last_name), ',', LOWER(first_name)) AS new_name_format
FROM patients
ORDER BY first_name DESC;
/*
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.
*/
SELECT
province_id,
SUM(height) AS sum_height
FROM patients
GROUP BY province_id
HAVING sum_height >= 7000;
-- Show the difference between the largest weight and smallest weight for patients with the last name 'Maroni'
SELECT MAX(weight) - MIN(weight)
FROM patients
WHERE last_name = 'Maroni';
/*
Show all of the days of the month (1-31) and how many admission_dates occurred on that day.
Sort by the day with most admissions to least admissions.
*/
SELECT
DAY(admission_date) AS day_number,
COUNT(patient_id) AS number_of_admissions
FROM admissions
GROUP BY day_number
ORDER BY number_of_admissions DESC;
-- Show all columns for patient_id 542's most recent admission_date.
SELECT *
FROM admissions
WHERE patient_id = 542
ORDER BY
admission_date DESC -- LIMIT 1;
/*
Show patient_id, attending_doctor_id, and diagnosis for admissions that match one of the two criteria:
1. patient_id is an odd number and attending_doctor_id is either 1, 5, or 19.
2. attending_doctor_id contains a 2 and the length of patient_id is 3 characters.
*/
SELECT
patient_id,
attending_doctor_id,
diagnosis
FROM admissions
WHERE
(
attending_doctor_id IN (1, 5, 19)
AND patient_id % 2 != 0
)
OR (
attending_doctor_id LIKE '%2%'
AND LEN(patient_id) = 3
);
-- Show first name and last name of patients who does not have allergies. (null)
SELECT
first_name,
last_name
FROM patients
WHERE allergies IS NULL;
-- Show first name of patients that start with the letter 'C'
SELECT first_name
FROM patients
WHERE first_name LIKE 'c%';
-- Show first name and last name of patients that weight within the range of 100 to 120 (inclusive)
SELECT
first_name,
last_name
FROM patients
WHERE weight >= 100 AND weight <= 120;
/*
Show first_name, last_name, and the total number of admissions attended for each doctor.
Every admission has been attended by a doctor.
*/
SELECT
first_name,
last_name,
COUNT(*)
FROM doctors
JOIN admissions ON admissions.attending_doctor_id = doctors.doctor_id
GROUP BY attending_doctor_id;
-- For each doctor, display their id, full name, and the first and last admission date they attended.
SELECT
doctor_id,
first_name || " " || last_name AS full_name,
MIN(admission_date) AS first_adm_date,
MAX(admission_date) AS last_adm_date
FROM doctors
JOIN admissions ON admissions.attending_doctor_id = doctors.doctor_id
GROUP BY attending_doctor_id;
-- Display the total amount of patients for each province. Order by descending.
SELECT
province_name,
COUNT(*) AS amount
FROM patients
JOIN province_names ON province_names.province_id = patients.province_id
GROUP BY province_name
ORDER BY amount DESC;
/*
For every admission, display the patient's full name, their admission diagnosis, and their doctor's full
name who diagnosed their problem.
*/
SELECT
p.first_name || " " || p.last_name AS patient_name,
diagnosis,
doctors.first_name || " " || doctors.last_name
FROM patients AS p
JOIN province_names ON province_names.province_id = p.province_id
JOIN admissions ON admissions.patient_id = p.patient_id
JOIN doctors ON admissions.attending_doctor_id = doctors.doctor_id;
/*
display the first name, last name and number of duplicate patients based on their first name and last name.
Ex: A patient with an identical name can be considered a duplicate.
*/
SELECT
first_name,
last_name,
COUNT(*) AS total
FROM patients
GROUP BY
first_name,
last_name
HAVING COUNT(*) > 1;
/*
Display patient's full name, height in the units feet rounded to 1 decimal, weight in the unit pounds rounded
to 0 decimals, birth_date, gender non abbreviated.
*/
UPDATE patients
SET gender = 'MALE'
WHERE gender = 'M';
UPDATE patients
SET gender = 'FEMALE'
WHERE gender = 'F';
SELECT
p.first_name || " " || p.last_name AS patient_name,
ROUND(height / 30.48, 1),
ROUND(weight * 2.205),
birth_date,
gender
FROM patients AS p;
/*
Show patient_id, first_name, last_name FROM patients whose does not have any records in the admissions table.
*/
SELECT
patient_id,
first_name,
last_name
FROM patients
WHERE patient_id NOT IN (
SELECT patient_id
FROM admissions
);
/*
Display a single row with max_visits, min_visits, average_visits where the maximum, minimum and average number
of admissions per day is calculated. Average is rounded to 2 decimal places.
*/
SELECT
MAX(daily_visits) AS max_visits,
MIN(daily_visits) AS min_visits,
ROUND(AVG(daily_visits), 2) AS avg_visits
FROM (
SELECT
admission_date,
COUNT(*) AS daily_visits
FROM admissions
GROUP BY admission_date
);
/*
Display every patient that has at least one admission and show their most recent admission along with the
patient and doctor's full name.
*/
SELECT
p.first_name || ' ' || p.last_name AS patient_name,
MAX(admission_date) AS most_recent_adm,
d.first_name || ' ' || d.last_name AS doctor_full_name
FROM patients p
JOIN admissions adm ON adm.patient_id = p.patient_id
JOIN doctors d ON d.doctor_id = adm.attending_doctor_id
GROUP BY adm.patient_id
HAVING COUNT(*) >= 1;
/*
Atividade de análise de quanto cada cidade gastou em cada produto, somando os valores das vendas e exibindo os
resultados do maior para o menor gasto, com ordenação adicional por cidade e produto.
*/
SELECT
ci.city_name,
p.product_name,
ROUND(SUM(ii.line_total_price), 2) AS total_spent
FROM city ci
JOIN customer cu ON cu.city_id = ci.id
JOIN invoice i ON i.customer_id = cu.id
JOIN invoice_item ii ON ii.invoice_id = i.id
JOIN product p ON p.id = ii.product_id
GROUP BY
ci.city_name,
p.product_name
ORDER BY
total_spent DESC,
ci.city_name ASC,
p.product_name ASC;