w3resource

SQL Exercise: Find nurses who have ever been on call for room 122

SQL hospital Database: Exercise-35 with Solution

35. From the following table, write a SQL query to find out, which nurses have been on call for room 122 in the past. Return name of the nurses.

Sample table: nurse
 employeeid |      name       |  position  | registered |    ssn
------------+-----------------+------------+------------+-----------
        101 | Carla Espinosa  | Head Nurse | t          | 111111110
        102 | Laverne Roberts | Nurse      | t          | 222222220
        103 | Paul Flowers    | Nurse      | f          | 333333330
Sample table: on_call
 nurse | blockfloor | blockcode |     oncallstart     |      oncallend
-------+------------+-----------+---------------------+---------------------
   101 |          1 |         1 | 2008-11-04 11:00:00 | 2008-11-04 19:00:00
   101 |          1 |         2 | 2008-11-04 11:00:00 | 2008-11-04 19:00:00
   102 |          1 |         3 | 2008-11-04 11:00:00 | 2008-11-04 19:00:00
   103 |          1 |         1 | 2008-11-04 19:00:00 | 2008-11-05 03:00:00
   103 |          1 |         2 | 2008-11-04 19:00:00 | 2008-11-05 03:00:00
   103 |          1 |         3 | 2008-11-04 19:00:00 | 2008-11-05 03:00:00
Sample table: room
roomnumber | roomtype | blockfloor | blockcode | unavailable
-----------+----------+------------+-----------+-------------
       101 | Single   |          1 |         1 | f
       102 | Single   |          1 |         1 | f
       103 | Single   |          1 |         1 | f
       111 | Single   |          1 |         2 | f
       112 | Single   |          1 |         2 | t
       113 | Single   |          1 |         2 | f
       121 | Single   |          1 |         3 | f
       122 | Single   |          1 |         3 | f
       123 | Single   |          1 |         3 | f
       201 | Single   |          2 |         1 | t
       202 | Single   |          2 |         1 | f
       203 | Single   |          2 |         1 | f
       211 | Single   |          2 |         2 | f
       212 | Single   |          2 |         2 | f
       213 | Single   |          2 |         2 | t
       221 | Single   |          2 |         3 | f
       222 | Single   |          2 |         3 | f
       223 | Single   |          2 |         3 | f
       301 | Single   |          3 |         1 | f
       302 | Single   |          3 |         1 | t
       303 | Single   |          3 |         1 | f
       311 | Single   |          3 |         2 | f
       312 | Single   |          3 |         2 | f
       313 | Single   |          3 |         2 | f
       321 | Single   |          3 |         3 | t
       322 | Single   |          3 |         3 | f
       323 | Single   |          3 |         3 | f
       401 | Single   |          4 |         1 | f
       402 | Single   |          4 |         1 | t
       403 | Single   |          4 |         1 | f
       411 | Single   |          4 |         2 | f
       412 | Single   |          4 |         2 | f
       413 | Single   |          4 |         2 | f
       421 | Single   |          4 |         3 | t
       422 | Single   |          4 |         3 | f
       423 | Single   |          4 |         3 | f

Sample Solution:

SELECT n.name
FROM nurse n
WHERE employeeid IN
    ( SELECT oc.nurse
     FROM on_call oc,
          room r
     WHERE oc.blockfloor = r.blockfloor
       AND oc.blockcode = r.blockcode
       AND r.roomnumber = 122 );

Sample Output:

      name
-----------------
 Laverne Roberts
 Paul Flowers
(2 rows)	

Explanation:

The said query in SQL that selects the name of the nurse who is on-call for the same block and floor as room number 122.

In the subquery the nurse ID from the 'on_call' table, which stores information about nurses who are on call. A join builds with the 'room' table and 'on_call' table, to match the block and floor of the room number 122 with the corresponding block and floor of the nurse's on-call shift.

The outer query then selects the name of the nurse using the nurse's employee ID, which is found in the 'nurse' table.

The WHERE clause filters the nurse records based on whether their employee ID is in the list of nurse IDs returned by the subquery.

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 SQL Exercise: Medical procedures done after certification expired.
Next SQL Exercise: Patients who have been prescribed some medication.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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://www.w3resource.com/sql-exercises/hospital-database-exercise/sql-exercise-hospital-database-35.php