Longest Consecutive Login Streak
Preview mode. Log in to edit, run, submit, and save progress.
Longest Consecutive Login Streak
You are given a Logins table recording user logins. A user may have logged in multiple times on the same date - these count as a single login day. Write a SQL query to find the longest consecutive calendar day streak for each user. Return (user_id, longest_streak) ordered by user_id. Table: Logins
| Column Name | Type | Description |
|---|---|---|
| user_id | INT | ID of the user |
| login_date | DATE | Date of login |
Examples
User 1 has two streaks: Jan 1-3 (length 3) and Jan 5-7 (length 3). User 2 has streaks: Jan 1 (length 1) and Jan 3-5 (length 3). Deduplicate dates first, then use ROW_NUMBER minus date offset to identify consecutive groups.
Approach hint
Start with a simple approach, explain the trade-off, then move toward a cleaner or more scalable solution.
Common mistake
Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.