Month-over-month growth
Task
From monthly_revenue, return each month, its revenue and the growth versus the previous month as a percentage rounded to 1 decimal place (NULL for the first month). Order by month. Columns: month, revenue, growth_pct.
Order matters: sort your rows as asked. Column names do not have to match. SQLite dialect.
Tables
monthly_revenue
month TEXT, revenue INTEGER
| month | revenue |
|---|---|
| 2026-01 | 2000 |
| 2026-02 | 2500 |
| 2026-03 | 2250 |
| 2026-04 | 3000 |
- LAG(revenue) OVER (ORDER BY month) gives the previous month's revenue.
- Integer division truncates in SQLite: multiply by 100.0, not 100.
Expected output
| month | revenue | growth_pct |
|---|---|---|
| 2026-01 | 2000 | NULL |
| 2026-02 | 2500 | 25.0 |
| 2026-03 | 2250 | -10.0 |
| 2026-04 | 3000 | 33.3 |
Show solution
SELECT month, revenue,
ROUND(100.0 * (revenue - LAG(revenue) OVER (ORDER BY month))
/ LAG(revenue) OVER (ORDER BY month), 1) AS growth_pct
FROM monthly_revenue
ORDER BY month;LAG reads the previous row in window order. The classic bug is integer division: 100 * 750 / 2250 is 33 in SQLite, PostgreSQL and SQL Server, not 33.3. Multiply by 100.0 to force decimal arithmetic.
Why this works
LAG reads the previous row in window order. The classic bug is integer division: 100 * 750 / 2250 is 33 in SQLite, PostgreSQL and SQL Server, not 33.3. Multiply by 100.0 to force decimal arithmetic.
The full explanation, with more examples, is in the question Month-over-month growth.