Month-over-Month Revenue Growth
Preview mode. Log in to edit, run, submit, and save progress.
Month-over-Month Revenue Growth
You are given a table of sales transactions. Each row has a sale date and a revenue amount. Write a SQL query to compute the total revenue for each month, the previous month's revenue (prev_revenue), and the month-over-month growth percentage defined as: mom_growth_pct = ROUND((total_revenue - prev_revenue) / prev_revenue * 100, 2) For the very first month, prev_revenue and mom_growth_pct should be NULL. Return month (as YYYY-MM), total_revenue, prev_revenue, and mom_growth_pct ordered by month. Table: Sales
| Column Name | Type | Description |
|---|---|---|
| id | INT | Primary key |
| sale_date | DATE | Date of the sale |
| revenue | INT | Revenue for this sale |
Examples
First group sales by month, then use LAG() window function to get the previous month's total, then compute the percentage change.
Approach hint
Start with a simple approach, explain the trade-off, then move toward a cleaner or more scalable solution.
Common mistake
Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.