AnalystPath

Lookup Reservoir Levels

SQLEasyJunior level~15 min

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.

readings
reservoir_idyearlevel_cm
12018100
7202030
13201940
lookups
reservoir_idyear
12019
22008
32009

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

Related SQL questions