Gaps and Islands in SQL: Find Streaks and Missing Ranges
Gaps and islands is the classic SQL pattern for finding runs of consecutive values (islands) and the holes between them (gaps): login streaks, uptime periods, missing invoice numbers. Learn the ROW_NUMBER subtraction trick step by step, the LAG alternative, the duplicate trap that splits a streak in two, and how it performed on a million rows.
- An island is a run of consecutive values; a gap is the missing range between two islands.
- The core trick:
value - ROW_NUMBER() OVER (ORDER BY value)stays constant inside an island, so you can GROUP BY it. - A duplicate login day created a fake extra one-day island in our test until we used DISTINCT or DENSE_RANK.
- On about one million rows, SQLite found 10,310 islands in 0.71 seconds.
“What is each user’s longest daily login streak?” “When was the service down?” “Which invoice numbers are missing?” These look like different questions, but they are all the same problem: data is ordered, and you need to find where consecutive runs start and stop. SQL folk call it gaps and islands, it appears constantly in analytics work and in SQL interviews, and once you see the trick it takes only a few lines.
All queries ran on SQLite 3.50.4. They use standard window functions and work in PostgreSQL, SQL Server, MySQL 8 and Oracle with small changes to date arithmetic, which we note but did not run on those systems.
What are gaps and islands?
Picture the days a user logged in: 1, 2, 3, then 5, 6, then 10, 11, 12, 13 November. The islands are 1 to 3, 5 to 6 and 10 to 13. The gaps are 4 November and 7 to 9 November. The same shape appears with sequence numbers, sensor readings, shift schedules and status changes.
Our login data
CREATE TABLE logins (user TEXT, day TEXT);
-- asha: 2026-11-01, 02, 03, 05, 06, 10, 10, 11, 12, 13 (10 November twice)
-- ben: 2026-11-02, 04, 05
Asha logged in twice on 10 November. Real event tables always contain duplicates like this, and they matter, as you will see.
The ROW_NUMBER subtraction trick
WITH d AS (SELECT DISTINCT user, day FROM logins),
r AS (
SELECT user, day,
julianday(day) - ROW_NUMBER() OVER (PARTITION BY user ORDER BY day) AS grp
FROM d
)
SELECT user, MIN(day) AS start, MAX(day) AS finish, COUNT(*) AS days
FROM r
GROUP BY user, grp
ORDER BY user, start;
-- ('asha', '2026-11-01', '2026-11-03', 3)
-- ('asha', '2026-11-05', '2026-11-06', 2)
-- ('asha', '2026-11-10', '2026-11-13', 4)
-- ('ben', '2026-11-02', '2026-11-02', 1)
-- ('ben', '2026-11-04', '2026-11-05', 2)
Five islands, exactly as expected. julianday turns a date into a day number in SQLite. In PostgreSQL you would write day - ROW_NUMBER() OVER (...) * INTERVAL '1 day', and in SQL Server DATEADD(day, -ROW_NUMBER() OVER (...), day). PARTITION BY user restarts the numbering for each user so streaks never cross from one person to another.
Why it works, step by step
| day | row number | day number minus row number |
|---|---|---|
| 2026-11-01 | 1 | 2461344.5 |
| 2026-11-02 | 2 | 2461344.5 |
| 2026-11-03 | 3 | 2461344.5 |
| 2026-11-05 | 4 | 2461345.5 |
| 2026-11-06 | 5 | 2461345.5 |
| 2026-11-10 | 6 | 2461348.5 |
| 2026-11-11 | 7 | 2461348.5 |
| 2026-11-12 | 8 | 2461348.5 |
| 2026-11-13 | 9 | 2461348.5 |
Inside an island, the date and the row number both go up by exactly one per row, so their difference does not change. When a gap appears, the date jumps by more than one while the row number still moves by one, so the difference jumps too. The actual value of the difference is meaningless (the .5 is just how Julian day numbers work); all that matters is that it is the same for every row in an island and different between islands. Grouping by it gives one row per island.
The duplicate trap
-- same query, but FROM logins instead of the DISTINCT CTE
-- ('asha', '2026-11-10', '2026-11-13', 4)
-- ('asha', '2026-11-10', '2026-11-10', 1) fake extra island
Without removing duplicates, the two rows for 10 November got row numbers 6 and 7. The first one produced a different difference from the rest of its island, so the query reported a bogus one-day island alongside the real four-day streak. The count for the real island happened to stay right, but a “number of streaks” metric would now be wrong. Always deduplicate first with SELECT DISTINCT or a GROUP BY.
DENSE_RANK: a fix in one word
julianday(day) - DENSE_RANK() OVER (PARTITION BY user ORDER BY day) AS grp
-- ... then COUNT(DISTINCT day) instead of COUNT(*)
-- same five islands as the DISTINCT version
DENSE_RANK gives equal days the same number with no gaps in the numbering, so duplicates land in the same island. Remember to count distinct days, or the duplicate inflates the island length.
The LAG method: flag the start of each island
WITH d AS (SELECT DISTINCT user, day FROM logins),
f AS (
SELECT user, day,
CASE WHEN julianday(day) - julianday(LAG(day) OVER (PARTITION BY user ORDER BY day)) = 1
THEN 0 ELSE 1 END AS new_island
FROM d
),
g AS (
SELECT user, day, SUM(new_island) OVER (PARTITION BY user ORDER BY day) AS island
FROM f
)
SELECT user, island, MIN(day), MAX(day), COUNT(*) FROM g
GROUP BY user, island ORDER BY user, island;
-- ('asha', 1, '2026-11-01', '2026-11-03', 3)
-- ('asha', 2, '2026-11-05', '2026-11-06', 2)
-- ('asha', 3, '2026-11-10', '2026-11-13', 4)
-- ('ben', 1, '2026-11-02', '2026-11-02', 1)
-- ('ben', 2, '2026-11-04', '2026-11-05', 2)
The second approach compares each row with the previous one using LAG. A row starts a new island when the gap from the previous row is not exactly one (or when there is no previous row, which gives NULL and falls into the ELSE). A running SUM of those flags numbers the islands 1, 2, 3. It is longer, but more flexible: you can change the rule to “a new session starts after 30 minutes of inactivity”, which the subtraction trick cannot express. The CASE WHEN guide covers the flag expression.
Finding the gaps themselves
WITH d AS (SELECT DISTINCT user, day FROM logins),
n AS (SELECT user, day, LEAD(day) OVER (PARTITION BY user ORDER BY day) AS nxt FROM d)
SELECT user, date(day, '+1 day') AS gap_start, date(nxt, '-1 day') AS gap_end,
CAST(julianday(nxt) - julianday(day) - 1 AS INT) AS days
FROM n WHERE julianday(nxt) - julianday(day) > 1;
-- ('asha', '2026-11-04', '2026-11-04', 1)
-- ('asha', '2026-11-07', '2026-11-09', 3)
-- ('ben', '2026-11-03', '2026-11-03', 1)
LEAD looks at the next row. Wherever the next value is more than one step away, the range between them is a gap. Date helpers like date(day, '+1 day') are covered in our SQL date functions guide.
The longest streak per user
SELECT user, MAX(days) FROM ( ...islands query... ) GROUP BY user;
-- ('asha', 4)
-- ('ben', 2)
Once islands are rows, any aggregate works on top: longest streak, current streak (the island whose end is today), average streak length or number of streaks per month.
Numbers instead of dates: missing invoice numbers
-- invoices: 1001, 1002, 1003, 1007, 1008, 1012
SELECT MIN(no), MAX(no) FROM (
SELECT no, no - ROW_NUMBER() OVER (ORDER BY no) AS g FROM invoices
) GROUP BY g;
-- [(1001, 1003), (1007, 1008), (1012, 1012)]
SELECT no + 1, nxt - 1 FROM (
SELECT no, LEAD(no) OVER (ORDER BY no) AS nxt FROM invoices
) WHERE nxt - no > 1;
-- [(1004, 1006), (1009, 1011)]
With integers the subtraction needs no date functions. Auditors ask for missing invoice numbers regularly. Note that gaps in an auto-increment id are normal (rolled-back inserts and deleted rows leave holes, as our auto increment guide shows), so only use this on numbers that are supposed to be continuous.
Performance on a million rows
We filled a table with the integers 0 to 999,999, removed every multiple of 97, and grouped islands with the subtraction trick:
SELECT COUNT(*) FROM (
SELECT g FROM (SELECT day - ROW_NUMBER() OVER (ORDER BY day) AS g FROM big)
GROUP BY g
);
-- (10310,) 0.71 s
About 990,000 rows produced 10,310 islands in 0.71 seconds with no index. The cost is one sort plus one pass, so an index on the ordered column helps most. One SQLite detail we hit: grouping directly on the window alias in the same SELECT (GROUP BY g next to ROW_NUMBER() ... AS g) fails with misuse of aliased window function g. Compute the window in a subquery or CTE first, then group in the outer query.
Real-world uses
- Product analytics: daily active streaks, churn after a broken streak.
- Monitoring: turning per-minute health checks into outage periods with start and end times.
- HR and scheduling: consecutive working days, overlapping shifts.
- Finance and audit: missing cheque or invoice numbers.
- Testing: grouping consecutive failures of a flaky test into incidents.
Final thoughts
Gaps and islands looks intimidating until you see the trick: subtract a row number from an ordered value, and every run of consecutive values shares the same result. Deduplicate first, use LAG with a running sum when “consecutive” needs a custom rule, and use LEAD to list the gaps. With those three patterns, streaks, outages and missing numbers all become a short, fast query.
Frequently asked questions
What is the gaps and islands problem in SQL?
It is the task of finding runs of consecutive values, called islands, and the missing ranges between them, called gaps, in ordered data such as dates or sequence numbers.
How does the ROW_NUMBER method work?
Inside a run of consecutive values, both the value and its row number rise by one per row, so their difference stays constant. Grouping by that difference gives one group per island.
Why does my streak split into two islands?
Usually because of duplicate values. Each duplicate gets its own row number and shifts the difference. Remove duplicates with DISTINCT first, or use DENSE_RANK and count distinct values.
How do I find missing numbers in a sequence?
Use LEAD to compare each value with the next one. Wherever the next value is more than one higher, the numbers between them are missing.
Which is better, ROW_NUMBER or LAG?
ROW_NUMBER subtraction is shorter for strict steps of one. LAG with a running sum handles custom rules, such as a new session after 30 minutes of inactivity.
Keep learning
Need help with testing, WordPress or SEO?
We help teams ship better software and grow their sites. Tell us what you are working on and we will suggest a clear next step.