SQL QuestSQL Interview Questions › Joins

Self-Join: Repeat Orders Within a Week

HardProQuerying BasicsJoinsDate Functions

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_idcustomer_idproductcategoryquantitypricetotalorder_datecountrystatus
11Laptop ProElectronics11299.991299.992024-01-15USAcompleted
22Wireless MouseElectronics249.9999.982024-01-16Canadacompleted
33Office ChairFurniture1349.99349.992024-01-17USAcompleted

Expected output: (Jan 15→Jan 18, gap 3) and (Jan 18→Jan 25, gap 7) both qualify

Hint

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

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

Airbnb · Amazon · Meta · Uber

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

Related questions

Multi-CTE Revenue PipelineHard · FreeEmployees Earning More Than ManagerHard · ProEmployees with Similar SalariesHard · ProMoving Average with Dynamic WindowHard · ProDetect Repeat Buyers Within 7 DaysHard · ProRecursive Team Size RollupHard · Pro

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