SQL business case: the monthly revenue report
Your manager wants the monthly revenue report rebuilt in SQL so it stops living in a spreadsheet. It has four columns that build on each other: revenue per month, the change on the previous month, the running total for the year, and each month's share of the total.
Four exercises, each adding one column to the one before. By the end you have written the report most analysts are asked for in their first week.
Step 1: Revenue per month
- Revenue report, step 1: revenue per monthYou are building the monthly revenue report. Start with the base: for each month that has paid orders, return the month as YYYY-MM and the total paid amount, in columns month and revenue.
Step 2: Change on the previous month
- Revenue report, step 2: change on the previous monthExtend the monthly report. For each month return month, revenue, the previous month's revenue as prev_revenue, and the difference as change (revenue minus prev_revenue). The first month has no previous month, so both extra columns are NULL there.
Step 3: Running total
- Revenue report, step 3: running totalAdd a year-to-date column. For each month return month, revenue and running_total: the sum of paid revenue from the first month up to and including that month.
Step 4: Share of the total
- Revenue report, step 4: share of the totalFinish the report. For each month return month, revenue and share_pct: that month's percentage of all paid revenue, rounded to 1 decimal.
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: customer value and segments (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.