Water Meters Over the Monthly Cap
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.
| meter_id | monthly_cap |
|---|---|
| 100 | 50 |
| 200 | 40 |
| 300 | 30 |
| reading_id | meter_id | flow_type | volume | read_day |
|---|---|---|---|---|
| 1 | 100 | Usage | 60 | 2023-01-10 00:00:00 |
| 2 | 100 | Usage | 70 | 2023-02-12 00:00:00 |
| 3 | 100 | Credit | 100 | 2023-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