Skip to content

Commit 8cf9ec0

Browse files
authored
Update all.sql
1 parent e711a50 commit 8cf9ec0

File tree

1 file changed

+78
-137
lines changed

1 file changed

+78
-137
lines changed

sql-practicecom/medium/all.sql

+78-137
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,20 @@ Show patient_id and first_name from patients where their first_name start and en
1818
at least 6 characters long.
1919
2020
select patient_id, first_name from patients where first_name like 's%s' and len(first_name)>=6
21-
22-
2321
------------------------------------------------------------------------------------------------------------
24-
2522
Show patient_id, first_name, last_name from patients whos diagnosis is 'Dementia'.
2623
2724
Primary diagnosis is stored in the admissions table.
2825
2926
select p.patient_id, first_name, last_name from patients p join admissions a on p.patient_id = a.patient_id
3027
where diagnosis = 'Dementia'
3128
32-
33-
34-
3529
------------------------------------------------------------------------------------------------------------
36-
3730
Display every patient's first_name.
3831
Order the list by the length of each name and then by alphbetically
3932

4033
select first_name from patients p order by len(first_name) , first_name
41-
42-
43-
4434
------------------------------------------------------------------------------------------------------------
45-
4635
Show the total amount of male patients and the total amount of female patients in the patients table.
4736
Display the two results in the same row.
4837

@@ -51,12 +40,6 @@ select
5140
sum(case when gender = 'F' then 1 end) as female_count
5241
from patients;
5342

54-
55-
56-
57-
58-
59-
6043
------------------------------------------------------------------------------------------------------------
6144
Show first and last name, allergies from patients which have allergies to either 'Penicillin' or 'Morphine'. Show results
6245
ordered ascending by allergies then by first_name then by last_name.
@@ -66,182 +49,133 @@ select first_name,last_name,allergies
6649
from patients where allergies in ('Penicillin' , 'Morphine')
6750

6851
order by allergies, first_name,last_name
69-
70-
71-
72-
73-
7452
------------------------------------------------------------------------------------------------------------
75-
76-
7753
Show patient_id, diagnosis from admissions. Find patients admitted multiple times for the same diagnosis.
7854

79-
80-
8155
select
8256
patient_id, diagnosis from admissions
8357
group by diagnosis,patient_id
8458
having count(*)>1
8559

86-
87-
88-
89-
9060
------------------------------------------------------------------------------------------------------------
91-
9261
Show the city and the total number of patients in the city.
9362
Order from most to least patients and then by city name ascending.
9463

95-
9664
select
9765

9866
city, count(patient_id) as x from patients
9967
group by city
10068
order by x desc, city
10169

102-
103-
104-
105-
106-
10770
------------------------------------------------------------------------------------------------------------
108-
109-
11071
Show first name, last name and role of every person that is either patient or doctor.
11172
The roles are either "Patient" or "Doctor"
11273

113-
11474
select
11575

11676
first_name,last_name,'Patient' from patients
11777
union all
11878

11979
select
120-
12180
first_name,last_name,'Doctor' from doctors
12281

123-
124-
125-
126-
------------------------------------------------------------------------------------------------------------
127-
82+
------------------------------------------------------------------------------------------------------------
12883
Show all allergies ordered by popularity. Remove NULL values from query.
12984

130-
131-
132-
13385
select
13486
allergies, count(*) as total_diagnosis
13587
from patients where allergies is not null group by allergies order by total_diagnosis desc
13688

137-
138-
139-
140-
141-
142-
14389
------------------------------------------------------------------------------------------------------------
14490

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.
14593
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
16597
16698
16799
------------------------------------------------------------------------------------------------------------
168100
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
169105

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
179109

180110
------------------------------------------------------------------------------------------------------------
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.
181112
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
192117
------------------------------------------------------------------------------------------------------------
118+
Show the difference between the largest weight and smallest weight for patients with the last name 'Maroni'
193119
194120
195-
196-
197-
198-
199-
200-
201-
202-
203-
204-
121+
select
122+
max(weight) - min(weight)
123+
from patients where last_name = 'Maroni'
205124
206125
------------------------------------------------------------------------------------------------------------
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.
207129
130+
select
208131
132+
day(admission_date) as day_number,
133+
count(admission_date) as number_of_admissions
209134
135+
from admissions
210136
211-
212-
213-
214-
215-
216-
137+
group by day(admission_date)
138+
order by number_of_admissions desc
217139
------------------------------------------------------------------------------------------------------------
140+
Show all columns for patient_id 542's most recent admission_date.
218141

142+
select * from admissions a where a.patient_id = 542 order by admission_date desc limit 1
219143

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)
228150

229151
------------------------------------------------------------------------------------------------------------
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.
230155

231156

157+
select
232158

159+
patient_id, attending_doctor_id, diagnosis
233160

161+
from admissions
234162

163+
where mod(patient_id,2) = 1 and attending_doctor_id in(1,5,19)
235164

236-
237-
165+
or attending_doctor_id like '%2%' and len(patient_id) = 3
238166

239167
------------------------------------------------------------------------------------------------------------
240168

169+
Show first_name, last_name, and the total number of admissions attended for each doctor.
241170

171+
Every admission has been attended by a doctor.
242172

173+
select
243174

175+
first_name, last_name, count(*)
176+
from doctors d join admissions a on d.doctor_id = a.attending_doctor_id
244177

178+
group by first_name,last_name
245179

246180

247181

@@ -250,44 +184,51 @@ allergies, count(*) as total_diagnosis
250184
------------------------------------------------------------------------------------------------------------
251185

252186

187+
For each doctor, display their id, full name, and the first and last admission date they attended.
253188

189+
select
254190

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
255193

256-
257-
258-
194+
group by d.doctor_id, first_name,last_name
259195

260196

261197
------------------------------------------------------------------------------------------------------------
262198

199+
Display the total amount of patients for each province. Order by descending.
263200

201+
select
264202

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
265206

266-
267-
268-
207+
order by x desc
269208

270209

271210
------------------------------------------------------------------------------------------------------------
211+
For every admission, display the patient's full name, their admission
212+
diagnosis, and their doctor's full name who diagnosed their problem.
272213

214+
select
273215

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
280220

281221

282222
------------------------------------------------------------------------------------------------------------
283223

284224

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
285229

230+
group by first_name,last_name
231+
having count(*) >1
286232

287-
288-
289-
290-
291-
292-
------------------------------------------------------------------------------------------------------------
233+
order by num_of_duplicates desc
293234

0 commit comments

Comments
 (0)