We use cookies for site analytics. Accept to help us understand how the site is used. See our Privacy Policy for details.
A hard sql problem, graded against 5 test cases (3 of them hidden).
Query-writing problems - joins, aggregation, window functions, and query tuning.
Reach for it when you see: A schema and a question about the data rather than a function signature.
More SQLproblems →```sql
WITH numbered AS (
SELECT user_id, login_date,
ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY login_date) AS rn
FROM Logins
),
groups AS (
SELECT user_id, COUNT(*) AS streak_len
FROM numbered
GROUP BY user_id, login_date - rn * INTERVAL '1 day'
)
SELECT DISTINCT user_id
FROM groups
WHERE streak_len >= 3
ORDER BY user_id;
```
Why this works: if dates are 2026-05-01, 02, 03 their row numbers within the partition are 1, 2, 3, so date - rn days is constant (2026-04-30 in all three cases). A gap (skipped day) bumps the offset, splitting a new group. Counting rows per group gives streak length.
This is the canonical SQL pattern for "find consecutive runs". Variants:
- "Consecutive identical values" - same trick on a non-date column.
- "At least N in a row" - filter on COUNT(*) >= N.
- "Longest streak per user" - MAX of streak_len per user.
The full reference solution in every supported language stays in the editor above - reveal it there once you have had a real attempt.
Read off this problem's own test suite, so these are the cases a submission actually has to survive.
These apply to the pattern as a whole, not just this problem.