sqlstreak

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_idstreak_startstreak_enddays
942026-02-042026-02-1310
1772026-02-282026-03-0910
82026-01-142026-01-218
2322026-03-172026-03-248
432026-01-252026-01-317

Try it: PostgreSQL playground · SQLite playground

How it works

  1. 1
    SELECT DISTINCT user_id, played_on

    One row per user per day, however many plays.

  2. 2
    ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY played_on)

    Each user's days numbered 1, 2, 3…

  3. 3
    played_on - row_number

    Constant inside a run of consecutive days, different for every run: the streak's label.

  4. 4
    GROUP BY user_id, streak

    One 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_onrnstreak_label
2026-01-1312026-01-12
2026-01-1422026-01-12
2026-01-1532026-01-12
2026-01-1642026-01-12
2026-01-1752026-01-12
2026-01-1862026-01-12
2026-01-2072026-01-13
2026-01-2182026-01-13
2026-01-2492026-01-15
2026-01-25102026-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

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.

Start free