AnalystPath

Water Meters Over the Monthly Cap

SQLMediumMid level~15 min

Problem

Table: `Meters`

```text
+-------------+------+
| Column Name | Type |
+-------------+------+
| meter_id | int |
| monthly_cap | int |
+-------------+------+
meter_id is the primary key for this table.
Each row gives the allowed monthly water volume (in litres) for a meter.
```

Table: `Readings`

```text
+-------------+----------+
| Column Name | Type |
+-------------+----------+
| reading_id | int |
| meter_id | int |
| flow_type | enum |
| volume | int |
| read_day | datetime |
+-------------+----------+
reading_id is the primary key for this table.
flow_type is an ENUM of ('Usage', 'Credit'). Only 'Usage' counts toward consumption; 'Credit' rows are refunds and must be ignored.
```

A water meter is flagged as over-cap if its total monthly 'Usage' volume exceeds its monthly_cap in two or more consecutive calendar months.

Write a solution to report the `meter_id` of all over-cap meters.

Return the result table in any order.

Tables

Example rows — the live problem includes the full dataset.

Meters
meter_idmonthly_cap
10050
20040
30030
Readings
reading_idmeter_idflow_typevolumeread_day
1100Usage602023-01-10 00:00:00
2100Usage702023-02-12 00:00:00
3100Credit1002023-02-15 00:00:00

Expected output

Your answer should return 1 row with the columns meter_id.

Starter code (SQL)

SELECT *
FROM Meters;

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