Practice 7: VIPs Who Stopped After Jan and Feb
Picking targets for a retention campaign: "Which VIP customers bought in January and again in February 2024, but placed no order at all in March?"
Requirements
- Bought in January: at least one order whose
ord_nostarts with202401and that is not canceled (status <> 'CANCELED'). - Bought in February: the same with
202402. - No order in March: no order at all whose
ord_nostarts with202403, whatever its status. - Of the customers meeting all three, return only those of grade
VIP.
Result format
| Column | Meaning |
|---|---|
cust_id | Customer number |
cust_nm | Customer name |
region | Region |
Order does not matter. The result has about 100 rows (it differs by DB).
My solution
My solution
SELECT DISTINCT o.cust_id
FROM orders o
WHERE o.ord_no >= '202401' AND o.ord_no < '202402'
AND o.status <> 'CANCELED'
Hints
- "In January and also in February" is the intersection
INTERSECT; "of those, not in March" is the differenceEXCEPT(Set operations, Comparing sets). Set operations remove duplicates, soDISTINCTis not needed. - A set operation returns customer numbers only. Add the name and region from
customerswithIN (…)or a join. - Three
EXISTS/NOT EXISTSconditions give the same result.