SQL Exercise: Name of the patients, their block, floor, room number
SQL hospital Database: Exercise-28 with Solution
28. From the following tables, write a SQL query to find the name of the patients, their block, floor, and room number where they admitted.
Sample table: staystayid | patient | room | start_time | end_time --------+-----------+------+---------------------+--------------------- 3215 | 100000001 | 111 | 2008-05-01 00:00:00 | 2008-05-04 00:00:00 3216 | 100000003 | 123 | 2008-05-03 00:00:00 | 2008-05-14 00:00:00 3217 | 100000004 | 112 | 2008-05-02 00:00:00 | 2008-05-03 00:00:00Sample table: patient
ssn | name | address | phone | insuranceid | pcp -----------+-------------------+--------------------+----------+-------------+----- 100000001 | John Smith | 42 Foobar Lane | 555-0256 | 68476213 | 1 100000002 | Grace Ritchie | 37 Snafu Drive | 555-0512 | 36546321 | 2 100000003 | Random J. Patient | 101 Omgbbq Street | 555-1204 | 65465421 | 2 100000004 | Dennis Doe | 1100 Foobaz Avenue | 555-2048 | 68421879 | 3Sample 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 p.name AS "Patient",
s.room AS "Room",
r.blockfloor AS "Floor",
r.blockcode AS "Block"
FROM stay s
JOIN patient p ON s.patient=p.ssn
JOIN room r ON s.room=r.roomnumber;
Sample Output:
Patient | Room | Floor | Block -------------------+------+-------+------- John Smith | 111 | 1 | 2 Random J. Patient | 123 | 1 | 3 Dennis Doe | 112 | 1 | 2 (3 rows)
Explanation:
The said query in SQL that retrieves the name of the patient, the room number they are staying in, and the floor and block of that room.
The JOIN keyword joins the patient table to the stay table based on the ssn and patient columns and the room table to the stay table based on the roomnumber and room columns.
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: Find the floor with the minimum available rooms.
Next SQL Exercise: Find the nurses and the block where they are booked.
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-28.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics