Chapter 9 · Interview Level
33. Customer Cohort Analysis by First Purchase Month
Hard40 XPE-Commerce
Group customers into cohorts based on the month of their first order. For each cohort, show the number of customers, total revenue from the cohort, and average revenue per customer. Use a CTE to find each customer's first order month (as YYYY-MM). Round revenue figures to 2 decimals and sort by cohort month.
Tables
- PKidINT
- first_nameTEXT
- last_nameTEXT
- emailTEXT
- phoneTEXT
- cityTEXT
- stateTEXT
- countryTEXT
- created_atTIMESTAMP
- PKidINT
- FKcustomer_id→ customers.idINT
- order_dateTEXT
- statusTEXT
- total_amountREAL
Need help?
Ask Sol
AI tutor — looks at your current query and nudges you, without spoiling the answer.
Solution
Try the hints first — solving it yourself is what makes it stick.