Rainwater Held by a Skyline
Problem
A city skyline is described by the table `Skyline`, where each row gives a `position` (left-to-right index) and the `wall_m` height in metres of a unit-wide rooftop ledge at that position. After a storm, water pools on the rooftops: at any position the standing water level equals the shorter of the tallest ledge to its left (inclusive) and the tallest ledge to its right (inclusive); the water depth at that position is that level minus the position's own ledge height. Return a single column `pooled_water` with the total volume of water pooled across the whole skyline.
Tables
Example rows — the live problem includes the full dataset.
| position | wall_m |
|---|---|
| 1 | 0 |
| 2 | 1 |
| 3 | 0 |
Expected output
Your answer should return 1 row with the columns pooled_water.
Starter code (SQL)
SELECT *
FROM Skyline;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