# How to find consecutive days (streaks) in SQL

Source: https://sqlstreak.com/sql/how-to/consecutive-days-streak · Tested on PostgreSQL 18.6 and SQLite 3.54.1; results generated 2026-11-22.


**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:

```sql
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:

```sql
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 (both engines):

| 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 |

### 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:

```sql
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:

```sql
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 (both engines):

| 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 |

(8 of 10 rows)

## How it works

- `SELECT DISTINCT user_id, played_on`: One row per user per day, however many plays.
- `ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY played_on)`: Each user's days numbered 1, 2, 3…
- `played_on - row_number`: Constant inside a run of consecutive days, different for every run: the streak's label.
- `GROUP BY user_id, streak`: One row per streak, with its first day, last day and length.

## 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.
