Students and Their Dorm Rooms
Problem
Table: `Student`
```text
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| student_id | int |
| given_name | varchar |
| family_name | varchar |
+-------------+---------+
student_id is the primary key for this table.
Each row stores a student's id and name.
```
Table: `RoomAssignment`
```text
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| assignment_id | int |
| student_id | int |
| building | varchar |
| room_no | varchar |
+---------------+---------+
assignment_id is the primary key for this table.
Each row records the dorm building and room number assigned to a student.
```
Write a query that returns the given name, family name, building, and room number for every student. If a student has not been assigned a room, return `NULL` for the building and room number.
Return the rows in any order.
**Example**
Input:
```text
Student:
+------------+------------+-------------+
| student_id | given_name | family_name |
+------------+------------+-------------+
| 1 | Maya | Cohen |
| 2 | Liam | Ortiz |
+------------+------------+-------------+
RoomAssignment:
+---------------+------------+------------+---------+
| assignment_id | student_id | building | room_no |
+---------------+------------+------------+---------+
| 1 | 2 | Maple Hall | 204 |
| 2 | 3 | Oak Hall | 118 |
+---------------+------------+------------+---------+
```
Output:
```text
+------------+-------------+------------+---------+
| given_name | family_name | building | room_no |
+------------+-------------+------------+---------+
| Maya | Cohen | NULL | NULL |
| Liam | Ortiz | Maple Hall | 204 |
+------------+-------------+------------+---------+
```
Maya has no room assignment, so her building and room number are `NULL`. The assignment for `student_id = 3` is ignored because no such student exists.
Tables
Example rows — the live problem includes the full dataset.
| student_id | given_name | family_name |
|---|---|---|
| 1 | Maya | Cohen |
| 2 | Liam | Ortiz |
| assignment_id | student_id | building | room_no |
|---|---|---|---|
| 1 | 2 | Maple Hall | 204 |
| 2 | 3 | Oak Hall | 118 |
Expected output
Your answer should return 2 rows with the columns given_name, family_name, building, room_no.
Starter code (SQL)
SELECT *
FROM Student;Solve this SQL question free
Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock