Interview Questions/SQL/Users With 3 or More Consecutive Login Days

Users With 3 or More Consecutive Login Days

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Users With 3 or More Consecutive Login Days

You are given a table of user login records. Each row records a user ID and the date they logged in. A user may have duplicate entries for the same date. Write a SQL query to find all user IDs who have logged in on at least 3 consecutive calendar days at any point. Duplicate login dates for the same user should be treated as a single login for that day. Return the result ordered by user_id. Table: Logins

Column NameTypeDescription
user_idINTID of the user
login_dateDATEDate the user logged in

Examples

Example 1

User 1 logged in on Jan 1, 2, 3 - 3 consecutive days. User 2 skipped Jan 2. User 3 logged in on 4 consecutive days. Users 1 and 3 qualify.

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.

query.sql