SQL Quest › SQL Interview Questions › Joins
Self-Join: Repeat Orders Within a Week
Find every pair of orders where the same customer placed another order within 1–7 days. The 'next' order shares a customer but is on a strictly later date no more than a week away — classic short-window repeat-purchase detection used at Meta and Amazon for engagement analytics.
Show customer_id, first_order_date, next_order_date, first_order_product, next_order_product. Order by customer_id, then first_order_date.
The pattern: self-join the orders table with two aliases. The join condition pins customer_id and constrains the day-gap to BETWEEN 1 AND 7 using julianday() arithmetic. CAST to INT to ignore the sub-day fractional component you'd get if order_date ever had a time portion.
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
orders
| order_id | customer_id | product | category | quantity | price | total | order_date | country | status |
|---|---|---|---|---|---|---|---|---|---|
| 1 | 1 | Laptop Pro | Electronics | 1 | 1299.99 | 1299.99 | 2024-01-15 | USA | completed |
| 2 | 2 | Wireless Mouse | Electronics | 2 | 49.99 | 99.98 | 2024-01-16 | Canada | completed |
| 3 | 3 | Office Chair | Furniture | 1 | 349.99 | 349.99 | 2024-01-17 | USA | completed |
Expected output: (Jan 15→Jan 18, gap 3) and (Jan 18→Jan 25, gap 7) both qualify
Hint
Concepts
SELECT JOIN Date Functions Self-Join DISTINCT
Practise the topic: SQL practice questions · JOIN practice · Date function practice · Advanced SQL interview questions
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