SQL · Module 16 · Lesson 31 min read

Gaps & Islands: Streaks and Missing Ranges

The problem

Given a sequence (dates, numbers), find:

  • Islands: runs of consecutive values (a 5-day logging streak, consecutive login days)
  • Gaps: missing values between them (days with no activity, missing invoice numbers)

The island trick ⭐ (value minus row_number)

For consecutive values, value - row_number() is constant within each run:

dayrow_numberday − rn
Nov 11Oct 31
Nov 22Oct 31
Nov 33Oct 31
Nov 54Nov 1
Nov 65Nov 1

Then GROUP BY that difference → each group is an island with min() start, max() end and count() length.

Steps:

  1. Get distinct values per entity (e.g. distinct dates per user)
  2. grp = value - row_number() OVER (PARTITION BY user ORDER BY value) (for dates: day - rn * INTERVAL '1 day', or day - rn::int)
  3. GROUP BY user, grp → start, end, length
  4. Longest streak = max(length); current streak = the island that ends today/yesterday

Finding gaps

  • With lead(): the gap is between value and next_value when next_value - value > 1
  • Or anti-join against generate_series of all expected values

Real-world uses

  • Login / habit / savings streaks (gamification!)
  • Machine downtime, sensor outages
  • Missing invoice or cheque numbers (audits)
  • Consecutive months a customer was subscribed
  • Merging overlapping booking periods

Example

SQL
-- Activity log for streaks: the days Asha logged expenses
CREATE TEMP TABLE activity (user_id INT, day DATE);
INSERT INTO activity VALUES
  (1,'2026-11-01'),(1,'2026-11-02'),(1,'2026-11-03'),
  (1,'2026-11-05'),(1,'2026-11-06'),
  (1,'2026-11-08'),(1,'2026-11-09'),(1,'2026-11-10'),(1,'2026-11-11'),
  (2,'2026-11-02'),(2,'2026-11-03'),(2,'2026-11-09');

-- Islands: each streak's start, end and length
WITH numbered AS (
  SELECT user_id, day,
         day - (row_number() OVER (PARTITION BY user_id ORDER BY day))::int AS grp
  FROM (SELECT DISTINCT user_id, day FROM activity) d
)
SELECT user_id, min(day) AS streak_start, max(day) AS streak_end, count(*) AS days
FROM numbered
GROUP BY user_id, grp
ORDER BY user_id, streak_start;
-- 1 | 2026-11-01 | 2026-11-03 | 3
-- 1 | 2026-11-05 | 2026-11-06 | 2
-- 1 | 2026-11-08 | 2026-11-11 | 4
-- 2 | 2026-11-02 | 2026-11-03 | 2
-- 2 | 2026-11-09 | 2026-11-09 | 1

-- Longest streak per user
WITH numbered AS (
  SELECT user_id, day, day - (row_number() OVER (PARTITION BY user_id ORDER BY day))::int AS grp
  FROM (SELECT DISTINCT user_id, day FROM activity) d
), islands AS (
  SELECT user_id, count(*) AS days FROM numbered GROUP BY user_id, grp
)
SELECT user_id, max(days) AS longest_streak FROM islands GROUP BY user_id ORDER BY user_id;
-- 1 → 4 days | 2 → 2 days

-- Gaps: days with no activity between active days (using lead)
SELECT user_id, day + 1 AS gap_start, next_day - 1 AS gap_end, next_day - day - 1 AS missing_days
FROM (
  SELECT user_id, day, lead(day) OVER (PARTITION BY user_id ORDER BY day) AS next_day
  FROM activity
) t
WHERE next_day - day > 1
ORDER BY user_id, day;
-- 1 | 2026-11-04 | 2026-11-04 | 1 …

-- Missing invoice numbers with generate_series
CREATE TEMP TABLE invoices (no INT);
INSERT INTO invoices VALUES (1001),(1002),(1003),(1005),(1006),(1009),(1010);
SELECT s AS missing_invoice
FROM generate_series((SELECT min(no) FROM invoices), (SELECT max(no) FROM invoices)) s
WHERE s NOT IN (SELECT no FROM invoices);           -- 1004, 1007, 1008