Find Churned Customers with No Orders in the Last 90 Days
Return the customers whose most recent order is more than 90 days before the last order in the data, with that last order date.
The challenge runs on the SQLabHub practice database (SQLite) and uses the orders table. You can solve it in the browser, with nothing to install.
Why this question comes up
The win-back team defines a churned customer as one with no order in the last 90 days. Listing them with their last order date, most recent first, gives the campaign its target list in priority order.
The task
Find customers who have not ordered in the last 90 days of the data, counting back from the most recent order date. Show each one with their last order date, most recent first.
A correct answer returns 98 rows with 2 columns: user_id, last_order.
Tables used in this challenge
orders 200 rows
id INTEGER (PK) · user_id INTEGER · total REAL · discount REAL · item_count INTEGER · status TEXT · order_date TEXT · shipped_date TEXT · shipping_city TEXT · shipping_country TEXT
| id | user_id | total | discount | item_count | status | order_date | shipped_date | shipping_city | shipping_country |
|---|---|---|---|---|---|---|---|---|---|
The first rows of each table are shown. See how all the tables relate on the database schema page.
Expected output
| user_id | last_order |
|---|---|
First 5 of 98 rows. This is the output of the model solution on the practice database.
What you practise
- Summarising many rows into one value with COUNT, SUM, AVG, MIN or MAX
- Grouping rows with GROUP BY to get one result row per group
- Filtering groups after aggregation with HAVING
- Using the result of one query inside another
- Working with dates in SQLite: strftime, julianday and DATE
- Sorting the result with ORDER BY
Hint and approach
Try the challenge first. Open these only when you need them.
Show the hint
Compare each customer's MAX(order_date) with 90 days before the latest order in the table, not with today's date
Show the step-by-step approach
How the model solution is built, in the order the database evaluates it:
- Read from the
orderstable. - Group the rows by
user_id, so the result has one row for each value. - For each group, compute
MAX(order_date). - Filter the groups in
HAVING: keep those whereMAX(order_date)is less thanDATE((SELECT MAX(order_date) FROM orders), '-90 day'). - The subquery in HAVING runs once and returns a single value. It works like this:
- Read from the
orderstable. - Aggregate over all the rows that are left: compute
MAX(order_date). With no GROUP BY, the result is a single row.
- Read from the
- Sort the result by
last_orderdescending, thenuser_id.
Show the model solution
SELECT user_id, MAX(order_date) AS last_order
FROM orders
GROUP BY user_id
HAVING MAX(order_date) < DATE((SELECT MAX(order_date) FROM orders), '-90 day')
ORDER BY last_order DESC, user_id;This query produced the expected output above. Yours does not have to match it word for word: any query that returns the same rows is correct.
What to watch for
The cutoff is measured from the latest order in the table, not from DATE('now'): this dataset ends in December 2024, so a cutoff based on today would mark every customer as churned. On a live table you would use the current date. Customers who never ordered are absent, because the query reads only the orders table.
- HAVING. Conditions on single rows belong in WHERE and conditions on aggregates in HAVING. Putting a row filter in HAVING works, but it groups rows that are about to be thrown away.
- GROUP BY. Every selected column must either be in GROUP BY or sit inside an aggregate. SQLite accepts other columns and returns an arbitrary value for them; most databases raise an error.
- Date functions. These date functions are SQLite syntax. MySQL uses DATE_FORMAT and DATEDIFF, PostgreSQL uses TO_CHAR, EXTRACT and date subtraction.
Related SQL challenges
- Find Products Sold in at Least 6 Different MonthsSimilar levelHard
- Count Orders in Each Customer's First 7 DaysSimilar levelHard
- Find Customers with an Above-Average Number of OrdersEasierMedium
- Find Products Rated Above the Overall AverageEasierMedium
- Count Orders per User in the Latest Year of DataEasierMedium
Keep learning
- Lesson: HAVING Clause
- Lesson: GROUP BY
- Lesson: Date Functions
- Lesson: SQL Subqueries
- All SQL practice challenges, by difficulty and topic
- SQL interview questions with answers
- SQL Playground: run any query on the same database