SQL Quest › SQL Interview Questions › Subqueries & CTEs
Three Login Days in Any 7-Day Window
The streak rule is too strict for the habit metric: a customer counts as active once they have logged in on at least 3 different days within any 7-day window — the days do not need to be consecutive. logins has one row per login (login_at is a timestamp).
Show exactly these 2 columns: name (from customers) and first_hit — the earliest day (YYYY-MM-DD) on which the customer completed 3 login days inside 7 days. Order by first_hit, then name.
A 7-day window means the first and the third login day are at most 6 days apart. Three logins on the same day are one login day, not three.
Solve it in the browser editor →
Runs on SQLite in your browser, graded against the expected result, no signup. A wrong answer gets a diagnosis, not just "incorrect".
Schema
logins
| login_id | customer_id | login_at |
|---|---|---|
| 1 | 1 | 2024-03-01 08:12:00 |
| 2 | 1 | 2024-03-02 09:40:00 |
| 3 | 4 | 2024-03-01 18:05:00 |
customers
| customer_id | name | signup_date | membership | total_orders | |
|---|---|---|---|---|---|
| 1 | John Smith | john.smith@email.com | 2023-01-15 | Gold | 15 |
| 2 | Emma Wilson | emma.wilson@email.com | 2023-03-20 | Silver | 8 |
| 3 | Michael Brown | michael.brown@email.com | 2023-02-10 | Gold | 12 |
Expected output: 7 customers, the first on 2024-03-03
Hint
Concepts
SELECT CTE Window Functions LAG Date Functions DISTINCT GROUP BY JOIN
Practise the topic: SQL practice questions · CTE practice · Window function practice · Date function practice · GROUP BY exercises · JOIN practice · Advanced SQL interview questions
Read the concept: What is a CTE?
In these company practice sets
A SQL Quest challenge matched to patterns reported for these companies — not a question any of them has published.
Related questions
Where would this cost you points in an interview?
Ten questions, no signup: a Skillmap across nine SQL skills and the one to fix first.
Take the readiness test