Lookup Reservoir Levels
Problem
A water authority stores the recorded level of each reservoir at the end of selected years. Analysts submit lookup requests for a reservoir in a given year; if no level was recorded for that reservoir-year, the answer is `0`.
Table: `readings`
| Column | Type |
|---------------|------|
| reservoir_id | int |
| year | int |
| level_cm | int |
`(reservoir_id, year)` is the primary key. Each row is the recorded end-of-year level (in cm) for a reservoir.
Table: `lookups`
| Column | Type |
|---------------|------|
| reservoir_id | int |
| year | int |
`(reservoir_id, year)` is the primary key. Each row is one request.
Write a query that, for every lookup, reports `reservoir_id`, `year`, and `level_cm`. If the reservoir-year is missing from `readings`, report its level as `0`. Return the rows in any order.
Tables
Example rows — the live problem includes the full dataset.
| reservoir_id | year | level_cm |
|---|---|---|
| 1 | 2018 | 100 |
| 7 | 2020 | 30 |
| 13 | 2019 | 40 |
| reservoir_id | year |
|---|---|
| 1 | 2019 |
| 2 | 2008 |
| 3 | 2009 |
Expected output
Your answer should return 7 rows with the columns reservoir_id, year, level_cm.
Starter code (SQL)
SELECT *
FROM readings;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