IntermediateWindow functions

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

monthrevenue
2026-012000
2026-022500
2026-032250
2026-043000
Expected output
monthrevenuegrowth_pct
2026-012000NULL
2026-02250025.0
2026-032250-10.0
2026-04300033.3
Show solution
SQL
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.

The full explanation, with more examples, is in the question Month-over-month growth.