HardSQL challenge #294GROUP BYDATEANALYTICS

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.

Solve This SQL Challenge

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

First 3 rows of the orders table
iduser_idtotaldiscountitem_countstatusorder_dateshipped_dateshipping_cityshipping_country
143321.680.259cancelled2024-04-24NULLDelhiIndia
2810.520.191delivered2024-03-152024-03-20ColumbusUSA
3136181.190.085delivered2023-02-152023-02-18ChicagoUSA

The first rows of each table are shown. See how all the tables relate on the database schema page.

Expected output

Expected output of the challenge
user_idlast_order
1272024-08-24
332024-08-23
612024-08-17
772024-08-16
1252024-08-12

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:

  1. Read from the orders table.
  2. Group the rows by user_id, so the result has one row for each value.
  3. For each group, compute MAX(order_date).
  4. Filter the groups in HAVING: keep those where MAX(order_date) is less than DATE((SELECT MAX(order_date) FROM orders), '-90 day').
  5. The subquery in HAVING runs once and returns a single value. It works like this:
    1. Read from the orders table.
    2. Aggregate over all the rows that are left: compute MAX(order_date). With no GROUP BY, the result is a single row.
  6. Sort the result by last_order descending, then user_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.
Solve This SQL Challenge

Keep learning