Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Assignment 6

1. Write a query to get the average bill_price for all bill_id above 970. (5 points)
SELECT AVG(bill_price) FROM bill
WHERE bill_id > 970

2. Write a query to calculate the average salary of male doctors whose Doctor id
less than 35. (5 points)
SELECT AVG(dc_salary) FROM doctor
WHERE dc_gender = 'Male' AND doctor_id < 35

3. Write a query to find the first name, last name of doctors and salaries of the
doctors who have a higher salary than the doctor whose last name is
‘Wyldish’. (5 points)

SELECT dc_f_name,dc_l_name,dc_salary FROM doctor

WHERE dc_salary > (SELECT dc_salary

FROM doctor

WHERE dc_l_name = 'Wyldish')


4. Write a SQL subquery to find the first name and last name of all doctors who
works as Neurologist. (5 points)
SELECT dc_f_name, dc_l_name, spzltn_name FROM doctor dc
INNER JOIN specialization sp ON sp.specialization_id = dc.splztn_id
WHERE splztn_id IN (SELECT specialization_id
FROM specialization
WHERE spzltn_name = 'Neurologist')
5. Write a SQL subquery to find the first name, last name and salary, which is
greater than the average salary of the doctors. (5 points)
SELECT dc_f_name,dc_l_name,dc_salary FROM doctor
WHERE dc_salary > (SELECT AVG(dc_salary)
FROM doctor)
6. Write a SQL subquery to find the first name of doctors, last name of doctors
and salary of doctors, which is equal to the minimum salary for this
specialization, he/she is working on. (5 points)

SELECT dc_f_name,dc_l_name,dc_salary, spzltn_name FROM doctor dc

INNER JOIN specialization sp ON sp.specialization_id = dc.splztn_id

WHERE dc_salary IN (SELECT MIN(dc_salary)

FROM doctor

GROUP BY splztn_id)

7. Write a SQL subquery to find all the information of the doctors who draws the
same salary as the minimum salary for all specializations. (10 points)

SELECT * FROM doctor

WHERE dc_salary IN (SELECT MIN(dc_salary)

FROM doctor)
8. Write a query to get three minimum salaries of doctors. (10 points)
SELECT dc_f_name,dc_l_name,dc_salary FROM doctor dc1
WHERE 3 >= (SELECT COUNT(dc_salary)
FROM doctor dc2
WHERE dc2.dc_salary <= dc1.dc_salary
ORDER BY dc1.dc_salary DESC)

9. Write a query that returns the first name, salary, gender of doctors, along with
the average salaries of each gender. Use the window function. (10 points)
SELECT dc_f_name,dc_salary,dc_gender,
AVG(dc_salary) OVER (
PARTITION BY dc_gender
)
FROM doctor
10. Write a query that filters and finds only the top paid doctor in each hospital
id. Use the window function. (10 points)
SELECT * FROM(
SELECT dc_f_name,dc_l_name,dc_salary,hospital_id,
rank() OVER (
PARTITION BY hospital_id
ORDER BY dc_salary DESC
)
FROM doctor)sub_query
WHERE rank = 1;
11.Write a SQL query using ROW_NUMBER window function. (10 points)

SELECT dc_f_name,dc_l_name,dc_salary,

ROW_NUMBER () OVER (

PARTITION BY hospital_id

ORDER BY dc_salary

FROM doctor
12.Write a SQL query using DENSE_RANK window function. (10 points)

SELECT dc_f_name,dc_l_name,dc_salary,hospital_id,

DENSE_RANK() OVER (

PARTITION BY hospital_id

ORDER BY dc_salary DESC

FROM doctor
13.Write a SQL query using FIRST_VALUE window function. (10 points)

SELECT dc_f_name,dc_l_name,dc_salary,hospital_id,

FIRST_VALUE(dc_salary) OVER (

PARTITION BY hospital_id

ORDER BY dc_salary

) AS lowest_salary_per_hospital

FROM doctor

You might also like