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
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
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;| user_id | login_date | rn | island |
|---|---|---|---|
| 1 | 2026-03-01 | 1 | 2026-02-28 |
| 1 | 2026-03-02 | 2 | 2026-02-28 |
| 1 | 2026-03-03 | 3 | 2026-02-28 |
| 1 | 2026-03-05 | 4 | 2026-03-01 |
| 1 | 2026-03-06 | 5 | 2026-03-01 |
| 2 | 2026-03-01 | 1 | 2026-02-28 |
| 2 | 2026-03-03 | 2 | 2026-03-01 |
| 2 | 2026-03-04 | 3 | 2026-03-01 |
| 3 | 2026-03-10 | 1 | 2026-03-09 |
| 3 | 2026-03-11 | 2 | 2026-03-09 |
| 3 | 2026-03-12 | 3 | 2026-03-09 |
| 3 | 2026-03-13 | 4 | 2026-03-09 |
Step 2: streaks of 3 or more days
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;| user_id | streak_start | streak_end | days |
|---|---|---|---|
| 1 | 2026-03-01 | 2026-03-03 | 3 |
| 3 | 2026-03-10 | 2026-03-13 | 4 |
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 - rnin PostgreSQL and DuckDB.
Common mistakes
- Skipping the deduplication step, so repeat logins on the same day break streaks.
- Using
LAGto flag breaks and then forgetting to turn the flags into a running group number (the other valid approach is a runningSUMof "new island" flags). - Using
RANKinstead ofROW_NUMBERon non-unique dates.
Related questions
More on this topic in the SQL Interview Questions guide, or browse all SQL questions.