Show Each Book's Shelf Code
Problem
A community library keeps a catalogue of every book it owns, plus a separate sheet that records the physical shelf code for books that have already been placed on the shelves.
Table: `books`
| Column | Type |
|------------|---------|
| book_id | int |
| title | varchar |
`book_id` is the primary key. Each row is one book in the catalogue.
Table: `shelf_placements`
| Column | Type |
|------------|---------|
| book_id | int |
| shelf_code | int |
`book_id` is the primary key. Each row gives the shelf code where a book was placed. A book that has not yet been shelved has no row here.
Write a query that lists the `shelf_code` and `title` of every book. If a book has not been placed on a shelf, show its shelf code as `null`. Return the rows in any order.
Tables
Example rows — the live problem includes the full dataset.
| book_id | title |
|---|---|
| 1 | The Quiet Tide |
| 7 | Mapping Rivers |
| 11 | Garden Almanac |
| book_id | shelf_code |
|---|---|
| 3 | 1 |
| 11 | 2 |
| 90 | 3 |
Expected output
Your answer should return 5 rows with the columns shelf_code, title.
Starter code (SQL)
SELECT *
FROM books;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