SQL business case: customer value and segments
Marketing wants to know who the valuable customers are before planning a campaign. You build it up from the orders: what each customer has spent, when they first and last ordered, which segment that puts them in, and how large each segment is.
Four exercises that end in a segment summary a marketing team can act on.
Step 1: Orders and spend per customer
- Customer value, step 1: orders and spend per customerYou are sizing up the customer base. For every customer with at least one paid order, return name, the number of paid orders as orders, and their total paid amount as total.
Step 2: First order, last order and the days between
- Customer value, step 2: first order, last order, days activeFor every customer with at least one paid order, return name, the date of their first paid order as first_order, their last paid order as last_order, and days_active: the whole number of days between the two (0 for a single order).
Step 3: Segment the customers
- Customer value, step 3: segment the customersLabel each customer with at least one paid order. Return name, total (their paid spend) and segment: 'high' for 400 or more, 'mid' for 150 or more, otherwise 'low'.
Step 4: Summarize the segments
- Customer value, step 4: summarize the segmentsRoll the segments up. For each segment from step 3 return segment, the number of customers in it as customers, and their combined paid spend as revenue.
Other plans and cases
- 14-day SQL interview sprint (41)
- SQL for data analyst interviews (24)
- SQL for BI analyst interviews (21)
- SQL for data engineer interviews (24)
- Product analytics SQL in four sessions (12)
- SQL traps and time periods in three sessions (10)
- Window functions in six sessions (18)
- SQL on messy data: one work week (16)
- PostgreSQL features in four sessions (11)
- SQL business case: the refund review (5)
- SQL business case: the payroll review (5)
- SQL business case: the monthly revenue report (4)
- SQL business case: product performance (4)
- All exercises by level
Your first 8 exercises are free, with no account, from any plan or level. Progress is kept in this browser; a free account keeps it on every device.