SQL Practice Part 6

Question 51:

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.

Solution:

with cte as
(
select *, (case when patient_id%2=0 then 'Yes' else 'No' end) as has_isurance,  (case when patient_id%2=0 then 10 else 50 end) as cost
from admissions
)
select has_isurance,sum(cost) as total_cost from cte
group by has_isurance

 

 

Question 52:

Show the percent of patients that have 'M' as their gender. Round the answer to the nearest hundreth number and in percent form.

Solution:

SELECT 
CONCAT(
    ROUND(
      (
        SELECT COUNT(*)
        FROM patients
        WHERE gender = 'M') / CAST(COUNT(*) as float),4) * 100,
    '%'
  ) as percent_of_male_patients
FROM patients;

 


Comments (0)