w3resource
SQL exercises

SQL exercises on hospital Database: Write a query in SQL to find the name of the patients and the number of physicians they have taken appointment.

SQL hospital Database: Exercise-13 with Solution

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

Sample table: appointment


Sample table: patient


Sample Solution:

SELECT p.name "Patient",
       count(t.patient) "Appointment for No. of Physicians"
FROM appointment t
JOIN patient p ON t.patient=p.ssn
GROUP BY p.name
HAVING count(t.patient)>=1;

Sample Output:

      Patient      | Appointment for No. of Physicians
-------------------+-----------------------------------
 Grace Ritchie     |                                 2
 John Smith        |                                 3
 Dennis Doe        |                                 3
 Random J. Patient |                                 1
(4 rows)

Practice Online


E R Diagram of Hospital Database:

E R Diagram: SQL Hospital Database.

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a query in SQL to obtain the name of the patients with their physicians by whom they got their preliminary treatement.
Next: Write a query in SQL to count number of unique patients who got an appointment for examination room C.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming