Streaks and gaps
Runs of days: first SELECT DISTINCT user_id, day, then work out DATE_SUB(day, INTERVAL ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY day) DAY). Days in a row get the same value, and grouping by it gives one run. Days missed: DATEDIFF(day, LAG(day) …) - 1.
The shop launched an app in December, and the events table records what users did in the app each day for the first two weeks. A colleague from marketing asks: "Who logged in the most days in a row? Let's give them a badge." This lesson solves "in a row" questions. It's also one of the questions that comes up most often in data analysis interviews.
Remove duplicates first
In this step you first turn the data into "who came on which day". Look at the first few records of user 3, Carla:
1SELECT day, action2FROM events3WHERE user_id = 34 AND day <= '2025-12-05'5ORDER BY id;
| day | action |
|---|---|
| 2025-12-04 | login |
| 2025-12-04 | view |
| 2025-12-05 | login |
| 2025-12-05 | view |
Log in once and look at a product once in a day, and that's two rows. To count days in a row, what you need is "the days she came", and each day should be just one row. So first remove duplicates with DISTINCT:
1SELECT DISTINCT day2FROM events3WHERE user_id = 34ORDER BY day;
| day |
|---|
| 2025-12-04 |
| 2025-12-05 |
| 2025-12-07 |
| 2025-12-08 |
| 2025-12-09 |
| 2025-12-10 |
| 2025-12-11 |
| 2025-12-12 |
Carla came on 8 days: the 4th and the 5th, not the 6th, and then every day from the 7th to the 12th.
events has 210 rows in all. How many rows does SELECT DISTINCT user_id, day FROM events give?
DISTINCT removes duplicates across user_id, day together: if the same person did several things on the same day, only one row is kept. The result has 110 rows, that is, 110 times that "someone came on some day".Date minus row number
This step is the key to the whole lesson. Sort the days someone came, number them 1, 2, 3 and so on, and then subtract that number from the date:
1WITH days AS (2 SELECT DISTINCT day3 FROM events4 WHERE user_id = 35)6SELECT day,7 ROW_NUMBER() OVER (ORDER BY day) AS n,8 DATE_SUB(day, INTERVAL ROW_NUMBER() OVER (ORDER BY day) DAY) AS grp9FROM days10ORDER BY day;
| day | n | grp |
|---|---|---|
| 2025-12-04 | 1 | 2025-12-03 |
| 2025-12-05 | 2 | 2025-12-03 |
| 2025-12-07 | 3 | 2025-12-04 |
| 2025-12-08 | 4 | 2025-12-04 |
| 2025-12-09 | 5 | 2025-12-04 |
| 2025-12-10 | 6 | 2025-12-04 |
| 2025-12-11 | 7 | 2025-12-04 |
| 2025-12-12 | 8 | 2025-12-04 |
Look at the grp column: the 4th and the 5th both come out as December 3, and the 7th to the 12th all come out as December 4.
The idea is simple: while the days are in a row, the date goes up by 1 each day and so does the number, so the difference stays the same. As soon as a day is missed, the date jumps by one extra day but the number doesn't, so the difference changes. That means rows with the same grp belong to the same run of days in a row.
| day | n | grp |
|---|---|---|
| 2025-12-04 | 1 | 2025-12-03 |
| 2025-12-05 | 2 | 2025-12-03 |
| 2025-12-07 | 3 | 2025-12-04 |
| 2025-12-08 | 4 | 2025-12-04 |
| 2025-12-09 | 5 | 2025-12-04 |
| … | … | 2025-12-04 |
Someone came on the 1st, 2nd, 3rd and 5th. The 5th is number 4. What day does the 5th - 4 give?
1-- the 1st - 1 = day 02-- the 2nd - 2 = day 03-- the 3rd - 3 = day 04-- the 5th - 4 = day
How long each run is
In this step you gather the days of each run together. Group the rows with the same grp: the earliest day is the start, the latest day is the end, and COUNT(*) is how many days in a row. Add PARTITION BY user_id, and each person is numbered separately, so you can work it out for everyone at once:
1WITH days AS (2 SELECT DISTINCT user_id, day3 FROM events4),5grouped AS (6 SELECT user_id, day,7 DATE_SUB(day, INTERVAL ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY day) DAY) AS grp8 FROM days9)10SELECT user_id, MIN(day) AS start_day, MAX(day) AS end_day, COUNT(*) AS days11FROM grouped12WHERE user_id = 313GROUP BY user_id, grp14ORDER BY start_day;
| user_id | start_day | end_day | days |
|---|---|---|---|
| 3 | 2025-12-04 | 2025-12-05 | 2 |
| 3 | 2025-12-07 | 2025-12-12 | 6 |
Carla has two runs: 2 days and 6 days.
4 days in a row or more
✓Find every run where someone came 4 days in a row or more. Four columns: user_id, start_day, end_day and days, sorted by days from most to fewest, with ties sorted by user_id.
| user_id | start_day | end_day | days |
|---|---|---|---|
| 3 | 2025-12-07 | 2025-12-12 | 6 |
| 13 | 2025-12-09 | 2025-12-12 | 4 |
| 15 | 2025-12-11 | 2025-12-14 | 4 |
| 20 | 2025-12-10 | 2025-12-13 | 4 |
1WITH days AS (2 SELECT DISTINCT user_id, day3 FROM events4),5grouped AS (6 SELECT user_id, day,7 DATE_SUB(day, INTERVAL ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY day) DAY) AS grp8 FROM days9)10SELECT user_id11FROM grouped;
Why you must remove duplicates
In this step you see what happens if you forget DISTINCT. Number every row of events as it is:
Carla has two rows on the 4th. Numbering without removing duplicates, do the 4th and the 5th still get the same grp?
1SELECT day,2 DATE_SUB(day, INTERVAL ROW_NUMBER() OVER (ORDER BY day, id) DAY) AS grp3FROM events4WHERE user_id = 35 AND day <= '2025-12-05'6ORDER BY id;
Each person's longest run
In this step you answer the marketing question: what's each person's longest run of days in a row? Start from "each run", then take the MAX for each person:
1WITH days AS (2 SELECT DISTINCT user_id, day3 FROM events4),5grouped AS (6 SELECT user_id, day,7 DATE_SUB(day, INTERVAL ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY day) DAY) AS grp8 FROM days9),10runs AS (11 SELECT user_id, grp, COUNT(*) AS days12 FROM grouped13 GROUP BY user_id, grp14)15SELECT u.name, MAX(r.days) AS longest16FROM runs AS r17JOIN users AS u ON u.id = r.user_id18GROUP BY r.user_id, u.name19ORDER BY longest DESC, r.user_id20LIMIT 5;
| name | longest |
|---|---|
| Carla | 6 |
| Mateo | 4 |
| Otto | 4 |
| Uma | 4 |
| Diego | 3 |
Carla's longest run is 6 days in a row, so the badge goes to her. Note: she came on 8 days in all, but "came on the most days" and "longest run" are two different things. Mateo (user 13) came on 9 days, yet his longest run is only 4 days.
Put these steps in order to work out each person's longest run from events. Each step uses the result of the step before.
grouped AS (SELECT user_id, day, DATE_SUB(day, INTERVAL ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY day) DAY) AS grp FROM days),SELECT user_id, MAX(days) AS longest FROM runs GROUP BY user_id ORDER BY longest DESC, user_id LIMIT 3;WITH days AS (SELECT DISTINCT user_id, day FROM events),runs AS (SELECT user_id, grp, COUNT(*) AS days FROM grouped GROUP BY user_id, grp)
user_id | longest 3 | 6 13 | 4 15 | 4
WITH gives each step a name, so it's easy to read and easy to debug.How many days were missed
In this step you look at it the other way round, at the gaps: how many days went by between two visits. Use LAG from lesson 6 to get the previous day the person came. The difference in days minus 1 is how many days they missed in between:
1WITH days AS (2 SELECT DISTINCT user_id, day3 FROM events4)5SELECT user_id,6 LAG(day) OVER (PARTITION BY user_id ORDER BY day) AS prev_day,7 day,8 DATEDIFF(day, LAG(day) OVER (PARTITION BY user_id ORDER BY day)) - 1 AS missed9FROM days10WHERE user_id = 311ORDER BY day;
| user_id | prev_day | day | missed |
|---|---|---|---|
| 3 | NULL | 2025-12-04 | NULL |
| 3 | 2025-12-04 | 2025-12-05 | 0 |
| 3 | 2025-12-05 | 2025-12-07 | 1 |
| 3 | 2025-12-07 | 2025-12-08 | 0 |
| 3 | 2025-12-08 | 2025-12-09 | 0 |
| 3 | 2025-12-09 | 2025-12-10 | 0 |
| 3 | 2025-12-10 | 2025-12-11 | 0 |
| 3 | 2025-12-11 | 2025-12-12 | 0 |
A missed of 0 means they came the day before too. Carla only missed 1 day, the 6th. People with long gaps may be about to stop using the app, so they're worth a reminder.
People who came back after a long gap
✓Find the records where 5 or more days went by between two visits. Four columns: user_id, prev_day, day and missed, sorted by missed from most to fewest, with ties sorted by user_id.
| user_id | prev_day | day | missed |
|---|---|---|---|
| 22 | 2025-12-02 | 2025-12-12 | 9 |
| 9 | 2025-12-01 | 2025-12-08 | 6 |
| 6 | 2025-12-03 | 2025-12-09 | 5 |
| 14 | 2025-12-04 | 2025-12-10 | 5 |
| 16 | 2025-12-04 | 2025-12-10 | 5 |
| 21 | 2025-12-06 | 2025-12-12 | 5 |
1WITH days AS (2 SELECT DISTINCT user_id, day3 FROM events4),5gaps AS (6 SELECT user_id, day7 FROM days8)9SELECT user_id, prev_day, day, missed10FROM gaps11ORDER BY missed DESC, user_id;
What you learned
- Remove duplicates before counting days in a row:
SELECT DISTINCT user_id, day, so each day is one row. - Date minus row number:
DATE_SUB(day, INTERVAL ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY day) DAY). Days in a row get the same value. - Group by
user_id, grp:MIN(day)is the start,MAX(day)is the end andCOUNT(*)is how many days in a row. Then take theMAXfor each person to get their longest run. - Coming on many days isn't the same as having a long run.
- Gaps:
DATEDIFF(day, LAG(day) OVER (…)) - 1is how many days were missed in between. - Write it step by step in
WITH, and you can run each step on its own to check it.