← All lessons
Advanced SQL · Lesson 7 of 11

Streaks and gaps

logins has one row per customer per day they signed in. Questions like "what's each customer's longest streak of days in a row?" are known as gaps and islands: the islands are runs of consecutive days, the gaps are the days between. They're a favourite in interviews, and they come up for real with sessions, outages and subscriptions.

Gaps first. LAG gives the previous login day, and the difference in days says how long the gap was. Anything over 1 means at least one day was missed:

SELECT customer_id, day,
  julianday(day) - julianday(LAG(day) OVER (PARTITION BY customer_id ORDER BY day)) AS gap
FROM logins
WHERE customer_id = 1
ORDER BY day;
Returns
customer_id | day        | gap
------------+------------+-----
1           | 2024-10-01 | NULL
1           | 2024-10-02 | 1
1           | 2024-10-03 | 1
1           | 2024-10-05 | 2
1           | 2024-10-06 | 1
1           | 2024-10-09 | 3
(6 rows)

Now islands. The trick: number each customer's days 1, 2, 3 with ROW_NUMBER, and take that number away from the date. Along a run of consecutive days, the date and the number both go up by 1, so the difference stays the same. When a day is skipped, the date jumps by 2 and the difference changes. So the difference labels each island:

SELECT customer_id, day,
  ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY day) AS n,
  date(day, '-' || ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY day) || ' days') AS island
FROM logins
WHERE customer_id = 1
ORDER BY day;
Returns
customer_id | day        | n | island
------------+------------+---+-----------
1           | 2024-10-01 | 1 | 2024-09-30
1           | 2024-10-02 | 2 | 2024-09-30
1           | 2024-10-03 | 3 | 2024-09-30
1           | 2024-10-05 | 4 | 2024-10-01
1           | 2024-10-06 | 5 | 2024-10-01
1           | 2024-10-09 | 6 | 2024-10-03
(6 rows)

Group by that label and each group is one streak, with its first day, last day and length:

WITH labelled AS (
  SELECT customer_id, day,
    julianday(day) - ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY day) AS island
  FROM logins
)
SELECT customer_id, MIN(day) AS starts, MAX(day) AS ends, COUNT(*) AS days
FROM labelled
GROUP BY customer_id, island
ORDER BY customer_id, starts;
Returns
customer_id | starts     | ends       | days
------------+------------+------------+-----
1           | 2024-10-01 | 2024-10-03 | 3
1           | 2024-10-05 | 2024-10-06 | 2
1           | 2024-10-09 | 2024-10-09 | 1
2           | 2024-10-01 | 2024-10-02 | 2
2           | 2024-10-04 | 2024-10-07 | 4
3           | 2024-10-03 | 2024-10-03 | 1
4           | 2024-10-08 | 2024-10-09 | 2
(7 rows)

The trick needs one row per customer per day. If the same day could appear twice, take DISTINCT days first. To list the missing days rather than the streaks, generate a calendar with a recursive CTE (last lesson) and keep the days with no login, using EXCEPT or a LEFT JOIN.

Practice

Every login with the same customer's previous login day: customer_id, day and the previous day (NULL for their first).

The tables
  • customers (id, name, city, joined)
  • products (id, name, category, price)
  • orders (id, customer_id, ordered_on, status)
  • order_items (order_id, product_id, qty)
  • signups (id, email, name, signed_up_at)
  • payments (id, order_id, amount, currency, paid_at)
  • events (id, received_at, payload (JSON text))
  • staff (id, name, role, manager_id, salary, hired)
  • logins (customer_id, day)
More info

Quick reference

gaps and islandsRuns of consecutive values (islands) and what's between (gaps).
day - LAG(day)The gap since the previous row.
day - ROW_NUMBER()Constant along a run: group by it for streaks.
calendar EXCEPT dataThe missing days.

The other AI and data engineering courses