Skip to main content
10

Longest login streak

Hardgaps and islandsROW_NUMBERdates

Your code isn't saved yet: refreshing or leaving the page resets it. Saved progress comes with user accounts.

The logins table has a row for every login, and a user can log in several times on the same day.

Write a query that returns each user's longest streak: the largest number of consecutive days on which they logged in at least once. Return the columns user_id and longest_streak, one row per user who has logged in. Rows can be returned in any order.

Example

logins

user_idlogin_date
12024-03-01
12024-03-02
12024-03-03
12024-03-05
12024-03-06
22024-03-01
22024-03-03

Result:

user_idlongest_streak
13
21

User 1 logged in March 1 to 3 (3 days), missed the 4th, then logged in the 5th and 6th (2 days).

Tip

Number each user's login days 1, 2, 3, ... in date order. What happens to login_date - number inside a run of consecutive days?

Schema

sql
-- One row per login. A user can log in several times a day.
CREATE TABLE logins (
  user_id    INT NOT NULL,
  login_date DATE NOT NULL
);

Approach: gaps and islands

A run of consecutive values is called an island, and the trick for finding islands in SQL is worth memorizing. Number each user's days in order. Inside an island the date and the row number both go up by 1 each step, so their difference stays constant. After a gap the date jumps further than the row number, and the difference changes:

login_daterow_numberlogin_date - row_number
2024-03-0112024-02-29
2024-03-0222024-02-29
2024-03-0332024-02-29
2024-03-0542024-03-01
2024-03-0652024-03-01

That difference is an island id. Group by it, count the days, and take the maximum per user:

sql
WITH days AS (
  SELECT DISTINCT user_id, login_date
  FROM logins
),
islands AS (
  SELECT
    user_id,
    login_date - ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY login_date)::int AS island
  FROM days
)
SELECT user_id, MAX(streak) AS longest_streak
FROM (
  SELECT user_id, COUNT(*) AS streak
  FROM islands
  GROUP BY user_id, island
) streaks
GROUP BY user_id;
  • DISTINCT comes first: two logins on the same day would get two row numbers and break the pattern.
  • In Postgres, date - integer is a date. ROW_NUMBER returns a bigint, hence the ::int cast.
  • Month and leap-year boundaries just work, because the database does real date arithmetic.

An alternative with LAG

Mark the start of each island where the previous day isn't yesterday, then a running SUM of those marks numbers the islands:

sql
SELECT user_id, login_date,
       SUM(CASE WHEN login_date - prev = 1 THEN 0 ELSE 1 END)
         OVER (PARTITION BY user_id ORDER BY login_date) AS island
FROM (
  SELECT user_id, login_date,
         LAG(login_date) OVER (PARTITION BY user_id ORDER BY login_date) AS prev
  FROM (SELECT DISTINCT user_id, login_date FROM logins) d
) t;

It's longer, but adapts more easily to rules like "a gap of one day doesn't break the streak".

Follow-ups

  • Current streak: keep only the island that contains CURRENT_DATE (or yesterday).
  • The same pattern finds consecutive seats, uninterrupted uptime and ranges of ids.

Reference solution

sql
WITH days AS (
  SELECT DISTINCT user_id, login_date
  FROM logins
),
islands AS (
  SELECT
    user_id,
    login_date - ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY login_date)::int AS island
  FROM days
)
SELECT user_id, MAX(streak) AS longest_streak
FROM (
  SELECT user_id, COUNT(*) AS streak
  FROM islands
  GROUP BY user_id, island
) streaks
GROUP BY user_id;