w3resource

SQL Exercise: Find the floor and block with a given room number

SQL hospital Database: Exercise-5 with Solution

5. From the following table, write a SQL query to locate the floor and block where room number 212 is located. Return block floor as "Floor" and block code as "Block".

Sample table: room


Sample Solution:


-- This SQL query selects the blockfloor and blockcode columns from the room table, aliasing blockfloor as "Floor" and blockcode as "Block", and retrieves data for room number 212.

SELECT blockfloor AS "Floor", -- Selects the blockfloor column from the room table and aliases it as "Floor"
       blockcode AS "Block" -- Selects the blockcode column from the room table and aliases it as "Block"
FROM room -- Specifies the table from which to retrieve data, in this case, the room table
WHERE roomnumber=212; -- Filters the rows to include only the room with room number 212


Sample Output:

 Floor | Block
-------+-------
     2 |     2
(1 row)

Explanation:

The said query in SQL that selects the "blockfloor" and "blockcode" columns from the 'room' table where the "roomnumber" is equal to 212.

The "blockfloor" column aliased as "Floor" and the "blockcode" column aliased as "Block".

Pictorial presentation:

Find the floor and block where the room number 212 belongs to

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: Patients with at least one physician appointment.
Next SQL Exercise: Count the number available rooms.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.