AnalystPath

Show Each Book's Shelf Code

SQLEasyJunior level~15 min

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.

books
book_idtitle
1The Quiet Tide
7Mapping Rivers
11Garden Almanac
shelf_placements
book_idshelf_code
31
112
903

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

Related SQL questions