SQL gaps and islands
Finds runs of overlapping rows ('islands') and the gaps between them: a row starts a new island when it begins after every earlier row has ended.
- Categoría
- Ventana
- Returns
island boundaries- In this category
- 30 of 79
Sintaxis
CASE WHEN d <= MAX(d_end) OVER (ORDER BY d ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) THEN 0 ELSE 1 END AS island_start
Soporte del motor
| Motor | Support | Notas |
|---|---|---|
| SQL Server | Sí | Supported under this name. |
| PostgreSQL | Sí | Supported under this name. |
| MySQL | Sí | MySQL 8.0+ |
| SQLite | Sí | SQLite 3.25+ |
| Oracle | Sí | Supported under this name. |
Ejemplos
Any engine
-- merge overlapping reservations into booked ranges
WITH flags AS (
SELECT room_id, start_day, end_day,
CASE WHEN start_day <= MAX(end_day) OVER (
PARTITION BY room_id ORDER BY start_day, end_day
ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING)
THEN 0 ELSE 1 END AS is_start
FROM bookings
), islands AS (
SELECT f.*, SUM(is_start) OVER (PARTITION BY room_id ORDER BY start_day, end_day
ROWS UNBOUNDED PRECEDING) AS island_id
FROM flags f
)
SELECT room_id, MIN(start_day) AS from_day, MAX(end_day) AS to_day
FROM islands
GROUP BY room_id, island_id;
Any engine
-- the gaps: where the next booking starts after this one ends
WITH n AS (
SELECT room_id, end_day,
LEAD(start_day) OVER (PARTITION BY room_id ORDER BY start_day) AS next_start
FROM bookings
)
SELECT room_id, end_day AS gap_from, next_start AS gap_to
FROM n
WHERE next_start > end_day;
Conviene saberlo
- Sum the island-start flag runningly to assign an island id: SUM(flag) OVER (ORDER BY d); rows sharing an id belong to the same island. Forgetting the running SUM is the most common bug.
- Compare against the largest end so far, not just the previous row's end: a long booking can cover several later ones. MAX(end_day) OVER (ORDER BY start_day ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) handles that; LAG(end_day) does not.
- This is the range-flavoured sibling of the streak pattern: streaks group equal values, gaps-and-islands groups overlapping ranges.
Questions about Gaps and Islands
How do I find the gaps instead of the islands?
LEAD on the start column: a gap exists where the next row's start is beyond the current end. That check is only safe on merged islands or non-overlapping rows; with nested bookings, run it over the islands query's output.
Why do my islands merge everything into one group?
The island flag is probably always 0 (the comparison treats touching rows as connected), or the running SUM is missing and the id never advances. Check both steps separately.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.