SQL Quest › SQL Interview Questions › Subqueries & CTEs

Three Login Days in Any 7-Day Window

HardProQuerying BasicsSubqueries & CTEsWindow FunctionsDate FunctionsAggregation & GroupingJoins

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_idcustomer_idlogin_at
112024-03-01 08:12:00
212024-03-02 09:40:00
342024-03-01 18:05:00

customers

customer_idnameemailsignup_datemembershiptotal_orders
1John Smithjohn.smith@email.com2023-01-15Gold15
2Emma Wilsonemma.wilson@email.com2023-03-20Silver8
3Michael Brownmichael.brown@email.com2023-02-10Gold12

Expected output: 7 customers, the first on 2024-03-03

Hint

This is a Pro challenge — the hint, the step-by-step tutor and the reference solution open in the app.

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

Amazon

A SQL Quest challenge matched to patterns reported for these companies — not a question any of them has published.

Related questions

Month-over-Month Volume GrowthHard · ProTop 3 Spending Categories per CountryHard · ProThree-Day Login StreaksHard · ProA Subquery That Returns One ValueEasy · FreeA Subquery That Returns a Set (IN)Easy · FreeGive a Query a Name (WITH)Easy · Free

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