Kodewick
Start learning →
Streaks and gaps7 / 12

Streaks and gaps

Lesson 7 of 12 · Part 3 · Analysing users · about 20 minutes

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:

main.sql
1SELECT day, action2FROM events3WHERE user_id = 34    AND day <= '2025-12-05'5ORDER BY id;
Output
dayaction
2025-12-04login
2025-12-04view
2025-12-05login
2025-12-05view
4 rows

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:

main.sql
1SELECT DISTINCT day2FROM events3WHERE user_id = 34ORDER BY day;
Output
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
8 rows

Carla came on 8 days: the 4th and the 5th, not the 6th, and then every day from the 7th to the 12th.

Choose one

events has 210 rows in all. How many rows does SELECT DISTINCT user_id, day FROM events give?

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:

main.sql
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;
Output
dayngrp
2025-12-0412025-12-03
2025-12-0522025-12-03
2025-12-0732025-12-04
2025-12-0842025-12-04
2025-12-0952025-12-04
2025-12-1062025-12-04
2025-12-1172025-12-04
2025-12-1282025-12-04
8 rows

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.

days
dayngrp
2025-12-0412025-12-03
2025-12-0522025-12-03
2025-12-0732025-12-04
2025-12-0842025-12-04
2025-12-0952025-12-04
……2025-12-04
The date and the number go up together, so the difference stays the same; miss a day, and the difference changes
Fill inTap one to fill a blank; tap a blank to take it back

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:

main.sql
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;
Output
user_idstart_dayend_daydays
32025-12-042025-12-052
32025-12-072025-12-126
2 rows

Carla has two runs: 2 days and 6 days.

Exercise 1

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.

It should print
user_idstart_dayend_daydays
32025-12-072025-12-126
132025-12-092025-12-124
152025-12-112025-12-144
202025-12-102025-12-134
4 rows
exercise1.sql
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;
Output
Press Run or Ctrl+Enter

Why you must remove duplicates

In this step you see what happens if you forget DISTINCT. Number every row of events as it is:

Choose oneA wrong guess is fine

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:

main.sql
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;
Output
namelongest
Carla6
Mateo4
Otto4
Uma4
Diego3
5 rows

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 in orderDrag, or use the arrows

Put these steps in order to work out each person's longest run from events. Each step uses the result of the step before.

  1. 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),
  2. SELECT user_id, MAX(days) AS longest FROM runs GROUP BY user_id ORDER BY longest DESC, user_id LIMIT 3;
  3. WITH days AS (SELECT DISTINCT user_id, day FROM events),
  4. runs AS (SELECT user_id, grp, COUNT(*) AS days FROM grouped GROUP BY user_id, grp)
Should print
user_id | longest
3 | 6
13 | 4
15 | 4

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:

main.sql
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;
Output
user_idprev_daydaymissed
3NULL2025-12-04NULL
32025-12-042025-12-050
32025-12-052025-12-071
32025-12-072025-12-080
32025-12-082025-12-090
32025-12-092025-12-100
32025-12-102025-12-110
32025-12-112025-12-120
8 rows

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.

Exercise 2

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.

It should print
user_idprev_daydaymissed
222025-12-022025-12-129
92025-12-012025-12-086
62025-12-032025-12-095
142025-12-042025-12-105
162025-12-042025-12-105
212025-12-062025-12-125
6 rows
exercise2.sql
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;
Output
Press Run or Ctrl+Enter

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 and COUNT(*) is how many days in a row. Then take the MAX for 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 (…)) - 1 is 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.
Practise what this lesson taught#011day − ROW_NUMBER()Three days in a rowBasic#012day − ROW_NUMBER() + MAX()Longest streakMini challenge#014day − ROW_NUMBER() + MIN/MAXEvery streakMini challengeAll 50 SQL problems →