SQL how-to
How to find consecutive days (streaks) in SQL
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Short answer
Number each user's distinct days in order and subtract that number from the date: within an unbroken run the result stays the same, so it labels the streak. Group by it to get each streak's start, end and length.
The task: Find runs of consecutive days, such as the longest stretch a user listened every day.
The five longest listening streaks
PostgreSQL
WITH days AS (
SELECT DISTINCT user_id, played_on FROM plays
), labelled AS (
SELECT user_id, played_on,
played_on - (ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY played_on))::int AS streak
FROM days
)
SELECT user_id, MIN(played_on) AS streak_start, MAX(played_on) AS streak_end, COUNT(*) AS days
FROM labelled
GROUP BY user_id, streak
ORDER BY days DESC, user_id, streak_start
LIMIT 5;SQLite
WITH days AS (
SELECT DISTINCT user_id, played_on FROM plays
), labelled AS (
SELECT user_id, played_on,
julianday(played_on) - ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY played_on) AS streak
FROM days
)
SELECT user_id, MIN(played_on) AS streak_start, MAX(played_on) AS streak_end, COUNT(*) AS days
FROM labelled
GROUP BY user_id, streak
ORDER BY days DESC, user_id, streak_start
LIMIT 5;Result (PostgreSQL and SQLite, identical): 5 rows
| user_id | streak_start | streak_end | days |
|---|---|---|---|
| 94 | 2026-02-04 | 2026-02-13 | 10 |
| 177 | 2026-02-28 | 2026-03-09 | 10 |
| 8 | 2026-01-14 | 2026-01-21 | 8 |
| 232 | 2026-03-17 | 2026-03-24 | 8 |
| 43 | 2026-01-25 | 2026-01-31 | 7 |
Try it: PostgreSQL playground · SQLite playground
How it works
- 1
SELECT DISTINCT user_id, played_onOne row per user per day, however many plays.
- 2
ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY played_on)Each user's days numbered 1, 2, 3…
- 3
played_on - row_numberConstant inside a run of consecutive days, different for every run: the streak's label.
- 4
GROUP BY user_id, streakOne row per streak, with its first day, last day and length.
More examples
See the trick on one user
While the days run on without a gap, date minus row number doesn't change; after a gap it jumps.
PostgreSQL
SELECT played_on,
ROW_NUMBER() OVER (ORDER BY played_on) AS rn,
played_on - (ROW_NUMBER() OVER (ORDER BY played_on))::int AS streak_label
FROM (SELECT DISTINCT played_on FROM plays WHERE user_id = 32) d
ORDER BY played_on
LIMIT 10;SQLite
SELECT played_on,
ROW_NUMBER() OVER (ORDER BY played_on) AS rn,
date(played_on, '-' || ROW_NUMBER() OVER (ORDER BY played_on) || ' days') AS streak_label
FROM (SELECT DISTINCT played_on FROM plays WHERE user_id = 32) d
ORDER BY played_on
LIMIT 10;Result (PostgreSQL and SQLite, identical): 10 rows
| played_on | rn | streak_label |
|---|---|---|
| 2026-01-13 | 1 | 2026-01-12 |
| 2026-01-14 | 2 | 2026-01-12 |
| 2026-01-15 | 3 | 2026-01-12 |
| 2026-01-16 | 4 | 2026-01-12 |
| 2026-01-17 | 5 | 2026-01-12 |
| 2026-01-18 | 6 | 2026-01-12 |
| 2026-01-20 | 7 | 2026-01-13 |
| 2026-01-21 | 8 | 2026-01-13 |
| 2026-01-24 | 9 | 2026-01-15 |
| 2026-01-25 | 10 | 2026-01-15 |
Try it: PostgreSQL playground · SQLite playground
Variations
Only streaks of 3 days or more
Add HAVING COUNT(*) >= 3 after the GROUP BY.
The current streak
Keep the streak whose streak_end is today (or yesterday, if today hasn't been played yet).
Common mistakes
Numbering plays instead of distinct days: two plays on one day break the arithmetic.
Using RANK or DENSE_RANK: ties change the numbering. ROW_NUMBER over distinct days is what makes it work.
Subtracting a number from date text in SQLite: convert with julianday() (or date modifiers) first.
PostgreSQL and SQLite
PostgreSQL subtracts an integer from a date directly; SQLite works on julianday() numbers or date() modifiers. The labels look different, but both versions found the same streaks.
Official documentation
Go deeper
- Guide: SQL interview questions: the core patterns
- Lesson: Cohorts and retention (Pro)
Make it stick in five minutes a day
“Cohorts and retention” teaches this step by step on a real database, with instant feedback. Part of sqlstreak Pro; the first lessons are free.