SQL Exercise: Count unique patients who came to examination room C
SQL hospital Database: Exercise-14 with Solution
14. From the following tables, write a SQL query to count the number of unique patients who have been scheduled for examination room 'C'. Return unique patients as "No. of patients got appointment for room C".
Sample table: appointmentappointmentid | patient | prepnurse | physician | start_dt_time | end_dt_time | examinationroom ---------------+-----------+-----------+-----------+---------------------+---------------------+----------------- 13216584 | 100000001 | 101 | 1 | 2008-04-24 10:00:00 | 2008-04-24 11:00:00 | A 26548913 | 100000002 | 101 | 2 | 2008-04-24 10:00:00 | 2008-04-24 11:00:00 | B 36549879 | 100000001 | 102 | 1 | 2008-04-25 10:00:00 | 2008-04-25 11:00:00 | A 46846589 | 100000004 | 103 | 4 | 2008-04-25 10:00:00 | 2008-04-25 11:00:00 | B 59871321 | 100000004 | | 4 | 2008-04-26 10:00:00 | 2008-04-26 11:00:00 | C 69879231 | 100000003 | 103 | 2 | 2008-04-26 11:00:00 | 2008-04-26 12:00:00 | C 76983231 | 100000001 | | 3 | 2008-04-26 12:00:00 | 2008-04-26 13:00:00 | C 86213939 | 100000004 | 102 | 9 | 2008-04-27 10:00:00 | 2008-04-21 11:00:00 | A 93216548 | 100000002 | 101 | 2 | 2008-04-27 10:00:00 | 2008-04-27 11:00:00 | B
Sample Solution:
-- SELECTing the count of distinct patients who got appointments for room C
SELECT COUNT(DISTINCT patient) AS "No. of patients got appointment for room C"
-- FROM appointment table
FROM appointment
-- WHERE clause filters appointments for room C
WHERE examinationroom = 'C';
Sample Output:
No. of patients got appointment for room C -------------------------------------------- 3 (1 row)
Explanation:
The said query in SQL that retrieves the number of unique patients who have had appointments in examination room C.
The WHERE statement ensures that only the appointments in examination room C are counted.
Alternative Solution:
Using Subquery in SELECT:
-- SELECTing the count of patients who got appointments for room C using a subquery in SELECT
SELECT (
SELECT COUNT(DISTINCT patient)
FROM appointment
WHERE examinationroom = 'C'
) AS "No. of patients got appointment for room C";
Explanation:
This solution uses a subquery in the SELECT clause to count the number of distinct patients who got appointments for room C.
Practice Online
E R Diagram of Hospital Database:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous SQL Exercise: Patients and doctors who gave them preliminary care.
Next SQL Exercise: Patients and the room number where they treated.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/sql-exercises/hospital-database-exercise/sql-exercise-hospital-database-14.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics