Write a SQL query using a CTE (Common Table Expression) to find customers
who placed at least one order in 2023 but no orders in 2024 — i.e. they
churned. Return their names ordered alphabetically.
Table: `orders`
| id | customer | order_date |
|---|---|---|
| 1 | Alice | 2023-03-10 |
| 2 | Bob | 2023-07-22 |
| 3 | Alice | 2024-01-15 |
| 4 | Carol | 2023-11-05 |
| 5 | Dave | 2023-06-30 |
| 6 | Dave | 2024-03-20 |
Alice and Dave ordered in 2024 → retained. Bob and Carol did not → churned.
Expected output (column: customer)
Bob
CarolHint — define two CTEs (
active_2023,active_2024), then LEFT JOINand filter on
IS NULL.
Sample tests