w3resource logo
SQL Exercises

SQL exercises on hospital Database: Basic, Subqueries, and Joins

SQL [39 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts.]

Sample Database: hospital

1. Write a query in SQL to find all the information of the nurses who are yet to be registered. Go to the editor

Sample table: nurse


Click me to see the solution

2. Write a query in SQL to find the name of the nurse who are the head of their department. Go to the editor

Sample table: nurse


Click me to see the solution

3. Write a query in SQL to obtain the name of the physicians who are the head of each department. Go to the editor

Sample table: physician


Sample table: department


Click me to see the solution

4. Write a query in SQL to count the number of patients who taken appointment with at least one physician. Go to the editor

Sample table: appointment


Click me to see the solution

5. Write a query in SQL to find the floor and block where the room number 212 belongs to. Go to the editor

Sample table: room


Click me to see the solution

6. Write a query in SQL to count the number available rooms. Go to the editor

Sample table: room


Click me to see the solution

7. Write a query in SQL to count the number of unavailable rooms. Go to the editor

Sample table: room


Click me to see the solution

8. Write a query in SQL to count the number of unavailable rooms. Go to the editor

Sample table: physician


Click me to see the solution

9. Write a query in SQL to obtain the name of the physicians who are trained for a special treatement. Go to the editor

Sample table: physician


Sample table: procedure


Click me to see the solution

10. Write a query in SQL to obtain the name of the physicians with department who are yet to be affiliated. Go to the editor

Sample table: physician


Sample table: procedure


Click me to see the solution

11. Write a query in SQL to obtain the name of the physicians who are not a specialized physician. Go to the editor

Sample table: physician


Click me to see the solution

12. Write a query in SQL to obtain the name of the patients with their physicians by whom they got their preliminary treatement. Go to the editor

Sample table: patient


Click me to see the solution

13. Write a query in SQL to find the name of the patients and the number of physicians they have taken appointment. Go to the editor

Sample table: appointment


Click me to see the solution

14. Write a query in SQL to count number of unique patients who got an appointment for examination room C. Go to the editor

Sample table: appointment


Click me to see the solution

15. Write a query in SQL to find the name of the patients and the number of the room where they have to go for their treatment. Go to the editor

Sample table: patient


Click me to see the solution

16. Write a query in SQL to find the name of the patients and the number of the room where they have to go for their treatment. Go to the editor

Sample table: nurse


Click me to see the solution

17. Write a query in SQL to find the name of the patients who taken the appointment on the 25th of April at 10 am, and also display their physician, assisting nurses and room no. Go to the editor

Sample table: patient


Click me to see the solution

18. Write a query in SQL to find the name of patients and their physicians who does not require any assistance of a nurse. Go to the editor

Sample table: patient


Click me to see the solution

19. Write a query in SQL to find the name of patients and their physicians who does not require any assistance of a nurse. Go to the editor

Sample table: patient


Click me to see the solution

20. Write a query in SQL to find the name of the patients who taken an advanced appointment, and also display their physicians and medication. Go to the editor

Sample table: patient


Click me to see the solution

21. Write a query in SQL to find the name and medication for those patients who did not take any appointment. Go to the editor

Sample table: patient


Click me to see the solution

22. Write a query in SQL to count the number of available rooms in each block. Go to the editor

Sample table: room


Click me to see the solution

23. Write a query in SQL to count the number of available rooms in each floor. Go to the editor

Sample table: room


Click me to see the solution

24. Write a query in SQL to count the number of available rooms for each block in each floor. Go to the editor

Sample table: room


Click me to see the solution

25. Write a query in SQL to count the number of unavailable rooms for each block in each floor. Go to the editor

Sample table: room


Click me to see the solution

26. Write a query in SQL to find out the floor where the maximum no of rooms are available. Go to the editor

Sample table: room


Click me to see the solution

27. Write a query in SQL to find out the floor where the minimum no of rooms are available. Go to the editor

Sample table: room


Click me to see the solution

28. Write a query in SQL to obtain the name of the patients, their block, floor, and room number where they are admitted. Go to the editor

Sample table: stay


Click me to see the solution

29. Write a query in SQL to obtain the nurses and the block where they are booked for attending the patients on call. Go to the editor

Sample table: nurse


Click me to see the solution

30. Write a query in SQL to make a report which will show -
a) name of the patient,
b) name of the physician who is treating him or her,
c) name of the nurse who is attending him or her,
d) which treatement is going on to the patient,
e) the date of release,
f) in which room the patient has admitted and which floor and block the room belongs to respectively. Go to the editor

Sample table: undergoes


Click me to see the solution

31. Write a SQL query to obtain the names of all the physicians performed a medical procedure but they are not ceritifed to perform. Go to the editor

Sample table: undergoes


Click me to see the solution

32. Write a query in SQL to obtain the names of all the physicians, their procedure, date when the procedure was carried out and name of the patient on which procedure have been carried out but those physicians are not cetified for that procedure. Go to the editor

Sample table: physician


Sample table: undergoes


Sample table: patient


Sample table: procedure


Click me to see the solution

33. Write a query in SQL to obtain the name and position of all physicians who completed a medical procedure with certification after the date of expiration of their certificate. Go to the editor

Sample table: physician


Sample table: undergoes


Sample table: trained_in


Click me to see the solution

34. Write a query in SQL to obtain the name of all those physicians who completed a medical procedure with certification after the date of expiration of their certificate, their position, procedure they have done, date of procedure, name of the patient on which the procedure had been applied and the date when the certification expired.  Go to the editor

Sample table: physician


Sample table: undergoes


Sample table: patient


Sample table: procedure


Click me to see the solution

35. Write a query in SQL to obtain the names of all the nurses who have ever been on call for room 122.  Go to the editor

Sample table: nurse


Sample table: on_call


Click me to see the solution

36. Write a query in SQL to Obtain the names of all patients who has been prescribed some medication by his/her physician who has carried out primary care and the name of that physician.  Go to the editor

Sample table: patient


Click me to see the solution

37. Write a query in SQL to obtain the names of all patients who has been undergone a procedure costing more than $5,000 and the name of that physician who has carried out primary care.  Go to the editor

Sample table: patient


Click me to see the solution

38. Write a query in SQL to Obtain the names of all patients who had at least two appointment where the nurse who prepped the appointment was a registered nurse and the physiccian who has carried out primary care.  Go to the editor

Sample table: appointment


Click me to see the solution

39. Write a query in SQL to Obtain the names of all patients whose primary care is taken by a physician who is not the head of any department and name of that physician along with their primary care physician.  Go to the editor

Sample table: patient


Sample table: department


Click me to see the solution

Practice Online