Longest login streak
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_id | login_date |
|---|---|
| 1 | 2024-03-01 |
| 1 | 2024-03-02 |
| 1 | 2024-03-03 |
| 1 | 2024-03-05 |
| 1 | 2024-03-06 |
| 2 | 2024-03-01 |
| 2 | 2024-03-03 |
Result:
| user_id | longest_streak |
|---|---|
| 1 | 3 |
| 2 | 1 |
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
-- 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_date | row_number | login_date - row_number |
|---|---|---|
| 2024-03-01 | 1 | 2024-02-29 |
| 2024-03-02 | 2 | 2024-02-29 |
| 2024-03-03 | 3 | 2024-02-29 |
| 2024-03-05 | 4 | 2024-03-01 |
| 2024-03-06 | 5 | 2024-03-01 |
That difference is an island id. Group by it, count the days, and take the maximum per user:
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;DISTINCTcomes first: two logins on the same day would get two row numbers and break the pattern.- In Postgres,
date - integeris adate.ROW_NUMBERreturns abigint, hence the::intcast. - 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:
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
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;