Peak Concurrency and Total Overlap of Maintenance Windows
Problem
An infrastructure team schedules maintenance windows in `maintenance_windows`. Each row has a `server_id`, an `opened_at` timestamp, and a `closed_at` timestamp. Windows on the same server can overlap in time.
For every server return two figures: `peak_concurrent_windows` — the maximum number of that server's windows that are simultaneously open at any instant — and `total_overlap_minutes` — the sum, over every ordered pair of overlapping windows on that server, of the length in whole minutes that the two windows share. A pair (A, B) overlaps when A opens before B (A.opened_at < B.opened_at) and A is still open when B opens (A.closed_at > B.opened_at); the shared length is MIN(A.closed_at, B.closed_at) minus MAX(A.opened_at, B.opened_at), truncated to whole minutes. A server with no overlapping pair reports `total_overlap_minutes` of 0.
Return one row per server and order by `server_id`.
Tables
Example rows — the live problem includes the full dataset.
| server_id | opened_at | closed_at |
|---|---|---|
| 1 | 2024-06-01 01:00:00 | 2024-06-01 03:00:00 |
| 1 | 2024-06-01 02:00:00 | 2024-06-01 04:00:00 |
| 1 | 2024-06-01 02:30:00 | 2024-06-01 05:00:00 |
Expected output
Your answer should return 3 rows with the columns server_id, peak_concurrent_windows, total_overlap_minutes.
Starter code (SQL)
SELECT *
FROM maintenance_windows;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