AdvancedGaps and islands

What is the gaps-and-islands problem? Find users with 3 or more consecutive login days.

Scenario

Product wants to reward users with a login streak of at least 3 days. Users can log in several times a day.

Interview answerTry answering first, then reveal

An island is a run of consecutive values; a gap is where the run breaks. Deduplicate to one row per user per day, then subtract ROW_NUMBER() days from each date: consecutive days produce the same result, which becomes the island key. Group by user and key and keep islands with COUNT(*) >= 3.

Explanation

For consecutive dates, the date and its row number both go up by 1 each row, so date - row_number stays constant. When a day is skipped the date jumps by 2 while the row number goes up by 1, so the difference changes and a new island starts.

Deduplicating first matters: two logins on the same day would get two row numbers and break the arithmetic. SELECT DISTINCT user_id, login_date fixes it.

The same pattern finds consecutive months of payment, runs of sensor readings above a threshold, or ranges of sequential IDs.

Worked example

Run on DuckDB when this page was built; the results shown are the real output.

Sample data
SQL
CREATE TABLE logins (user_id INT, login_ts TIMESTAMP);
INSERT INTO logins VALUES
  (1, '2026-03-01 08:00'), (1, '2026-03-02 09:10'), (1, '2026-03-02 21:40'),
  (1, '2026-03-03 07:30'), (1, '2026-03-05 10:00'), (1, '2026-03-06 10:00'),
  (2, '2026-03-01 12:00'), (2, '2026-03-03 12:00'), (2, '2026-03-04 12:00'),
  (3, '2026-03-10 18:00'), (3, '2026-03-11 18:00'), (3, '2026-03-12 18:00'),
  (3, '2026-03-13 18:00');

Step 1: the island key

SQL
WITH days AS (
  SELECT DISTINCT user_id, CAST(login_ts AS DATE) AS login_date FROM logins
)
SELECT user_id, login_date,
       ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY login_date) AS rn,
       login_date - CAST(ROW_NUMBER() OVER (PARTITION BY user_id
                                            ORDER BY login_date) AS INT) AS island
FROM days
ORDER BY user_id, login_date;
Result
user_idlogin_daternisland
12026-03-0112026-02-28
12026-03-0222026-02-28
12026-03-0332026-02-28
12026-03-0542026-03-01
12026-03-0652026-03-01
22026-03-0112026-02-28
22026-03-0322026-03-01
22026-03-0432026-03-01
32026-03-1012026-03-09
32026-03-1122026-03-09
32026-03-1232026-03-09
32026-03-1342026-03-09

Step 2: streaks of 3 or more days

SQL
WITH days AS (
  SELECT DISTINCT user_id, CAST(login_ts AS DATE) AS login_date FROM logins
),
islands AS (
  SELECT user_id, login_date,
         login_date - CAST(ROW_NUMBER() OVER (PARTITION BY user_id
                                              ORDER BY login_date) AS INT) AS island
  FROM days
)
SELECT user_id, MIN(login_date) AS streak_start, MAX(login_date) AS streak_end,
       COUNT(*) AS days
FROM islands
GROUP BY user_id, island
HAVING COUNT(*) >= 3
ORDER BY user_id, streak_start;
Result
user_idstreak_startstreak_enddays
12026-03-012026-03-033
32026-03-102026-03-134

User 1 logged in twice on 2 March but still has one 3-day streak. User 2's best run is 2 days, so user 2 is not returned.

In production

  • Use the user's local date, not UTC, if a "day" means their day. Converting timestamps is part of the logic, not a detail.
  • For a current streak on a large event table, keep a daily activity table (user, date) and compute streaks from that instead of from raw events.
  • Date arithmetic differs by engine: DATEADD(day, -rn, d) in SQL Server, date_sub(d, rn) in Spark SQL, d - rn in PostgreSQL and DuckDB.

Common mistakes

  • Skipping the deduplication step, so repeat logins on the same day break streaks.
  • Using LAG to flag breaks and then forgetting to turn the flags into a running group number (the other valid approach is a running SUM of "new island" flags).
  • Using RANK instead of ROW_NUMBER on non-unique dates.

More on this topic in the SQL Interview Questions guide, or browse all SQL questions.