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_no starts with 202401 and that is not canceled (status <> 'CANCELED').
  • Bought in February: the same with 202402.
  • No order in March: no order at all whose ord_no starts with 202403, whatever its status.
  • Of the customers meeting all three, return only those of grade VIP.

Result format

ColumnMeaning
cust_idCustomer number
cust_nmCustomer name
regionRegion

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 difference EXCEPT (Set operations, Comparing sets). Set operations remove duplicates, so DISTINCT is not needed.
  • A set operation returns customer numbers only. Add the name and region from customers with IN (…) or a join.
  • Three EXISTS / NOT EXISTS conditions give the same result.