Sql-Practice - Solution (By Balwant Singh)
Sql-Practice - Solution (By Balwant Singh)
com Solution
→Prepared by @balwant singh
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 between 100 and 120
Update the patients table for the allergies column. If the patient's allergies
is null then replace it with 'NKA'
update patients
set allergies = 'NKA'
where patient_id in (select patient_id from patients where allergies is null)
Show first name and last name concatinated into one column to show their
full name.
select concat(first_name," ",last_name) full_name
from patients
Show first name, last name, and the full province name of each patient.
select
first_name,last_name,pr.province_name
from patients
Show how many patients have a birth_date with 2010 as the birth year.
select count(patient_id)
from patients
Show the first_name, last_name, and height of the patient with the greatest
height.
select
first_name,last_name,height
from patients
where patient_id = (select patient_id from patients order by height desc limit 1)
Show all columns for patients who have one of the following patient_ids:
1,45,534,879,1000
select *
from patients
count(patient_id)
from admissions
Show all the columns from admissions where the patient was admitted and
discharged on the same day.
select *
from admissions
from admissions
distinct city
from patients
Medium ->
Show unique birth years from patients and order them by ascending.
select
distinct year(birth_date)
from patients
order by birth_date
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(*)=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
patients.patient_id,first_name,last_name
from patients
first_name
from patients
order by length(first_name),concat(first_name,last_name)
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 (select count(*) from patients where gender = "M") male, (select count(*) from patients
where gender = "F")
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
order by allergies,first_name,last_name
patient_id,primary_diagnosis
from admissions
group by primary_diagnosis,patient_id
having count(patient_id)>1
Show the city and the total number of patients in the city in the order from
most to least patients.
select
city,count(*)
from patients
group by city
Show first name, last name and role of every person that is either patient or
physician.
The roles are either "Patient" or "Physician"
select
first_name,last_name,'patient' role
from patients
union
select
first_name,last_name,'physician' as role
from physicians
Show all allergies ordered by popularity. Remove 'NKA' and NULL values
from query.
SELECT
allergies,
COUNT(*) AS total_diagnosis
FROM patients
WHERE
GROUP BY allergies
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
concat(upper(last_name),",",lower(first_name))
from patients
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)
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
day(admission_date) , count(admission_date)
from admissions
group by day(admission_date)
Show the patient_id, nursing_unit_id, room, and bed for patient_id 542's
most recent admission_date.
select
patient_id,nursing_unit_id,room,bed
from admissions
limit 1
nursing_unit_id , count()
from admissions
group by nursing_unit_id
patient_id,attending_physician_id,primary_diagnosis
from admissions
Hard ->
For example, if they weight 100 to 109 they are placed in the 100 weight
group, 110-119 = 110 weight group, etc.
SELECT
FROM patients t
GROUP BY FLOOR(t.weight/10)
patient_id,weight,height,
case
else 0
end
as isObese
from
patients
patients.patient_id,patients.first_name,patients.last_name,ph.specialty
from patients
All patients who have gone through admissions, can see their medical
documents on our site. Those patients are given a temporary password
after their first admission. Show the patient_id and temp_password.
distinct p.patient_id,concat(p.patient_id,len(p.last_name),year(p.birth_date))
from patients p
join
admissions a
on a.patient_id = p.patient_id
Each admission costs $50 for patients without insurance, and $10 for
patients with insurance. All patients with an even patient_id have
insurance.
Give each patient a 'Yes' if they have insurance, and a 'No' if they don't
have insurance. Add up the admission_total cost for each has_insurance
group.
select
case
end
as has_insurance,
sum(
case
end
as cost_after_insurance
from admissions
group by has_insurance
Show the provinces that has more patients identified as 'M' than 'F'. Must
only show full province_name
Select
p.province_name
from provinces p
group by p.province_name
having count(case when gender = "M" then 1 end)>count(case when gender = "F" then 1 end)
We are looking for a specific patient. Pull all columns for the patient who
matches the following criteria:
- First_name contains an 'r' after the first two letters.
- Identifies their gender as 'F'
- Born in February, May, or December
- Their weight would be between 60kg and 80kg
- Their patient_id is an odd number
- They are from the city 'Halifax'
select *
from patients
and patient_id%2<>0
Show the percent of patients that have 'M' as their gender. Round the
answer to the nearest hundreth number and in percent form.
SELECT
CONCAT( ROUND((
SELECT COUNT(*)
FROM patients
) / CAST(COUNT(*) as float),
4
) * 100,
'%'
) as percent_of_male_patients
FROM patients;
Show the patient_id and total_spent for patients who spent over 150 in
medication_cost. Sort by most total_spent to least total_spent.
select
patient_id,sum(medication_cost)
group by patient_id
having sum(medication_cost)>150
Provide the description of each item, along with the total cost of the
quantity on hand (rounded to the nearest whole dollar), and the associated
primary vendor. Sort the output by the most spent to the least spent on
inventory.
SELECT
i.item_description,
v.vendor_name
FROM items i
GROUP BY i.item_description