IntermediateWindow functions

How do you compute month-over-month revenue growth?

Scenario

A KPI tile shows revenue growth versus the previous month. There were no sales at all in March.

Interview answerTry answering first, then reveal

Aggregate to one row per month, then compare each month with LAG(revenue) OVER (ORDER BY month): (revenue - prev) / NULLIF(prev, 0). Build a month spine first so a month with no sales exists as zero; otherwise LAG compares April with February.

Explanation

LAG(x, n) returns the value from n rows earlier in the window order (default 1). It looks at rows, not calendar months, which is why missing months matter.

NULLIF(prev, 0) turns a zero denominator into NULL, so growth after a zero month is NULL rather than a divide-by-zero error. Multiply by 100.0 to avoid integer division.

The first example shows the wrong answer when March has no row: April is compared with February. The second builds a month spine with generate_series and fills March with 0.

Worked example

Run on DuckDB when this page was built; the results shown are the real output.

Sample data
SQL
CREATE TABLE sales (sale_date DATE, amount INT);
INSERT INTO sales VALUES
  ('2026-01-10', 400), ('2026-01-22', 600), ('2026-02-03', 700),
  ('2026-02-25', 500), ('2026-04-12', 900);

Wrong: LAG over the months that happen to exist

SQL
WITH monthly AS (
  SELECT CAST(date_trunc('month', sale_date) AS DATE) AS month, SUM(amount) AS revenue
  FROM sales GROUP BY 1
)
SELECT month, revenue,
       LAG(revenue) OVER (ORDER BY month) AS prev_revenue,
       ROUND(100.0 * (revenue - LAG(revenue) OVER (ORDER BY month))
             / NULLIF(LAG(revenue) OVER (ORDER BY month), 0), 1) AS growth_pct
FROM monthly ORDER BY month;
Result
monthrevenueprev_revenuegrowth_pct
2026-01-011000NULLNULL
2026-02-011200100020
2026-04-019001200-25

Right: a month spine, so March exists as zero

SQL
WITH months AS (
  SELECT CAST(m AS DATE) AS month
  FROM generate_series(DATE '2026-01-01', DATE '2026-04-01', INTERVAL 1 MONTH) AS t(m)
),
monthly AS (
  SELECT m.month, COALESCE(SUM(s.amount), 0) AS revenue
  FROM months m
  LEFT JOIN sales s ON date_trunc('month', s.sale_date) = m.month
  GROUP BY m.month
)
SELECT month, revenue,
       LAG(revenue) OVER (ORDER BY month) AS prev_revenue,
       ROUND(100.0 * (revenue - LAG(revenue) OVER (ORDER BY month))
             / NULLIF(LAG(revenue) OVER (ORDER BY month), 0), 1) AS growth_pct
FROM monthly ORDER BY month;
Result
monthrevenueprev_revenuegrowth_pct
2026-01-011000NULLNULL
2026-02-011200100020
2026-03-0101200-100
2026-04-019000NULL

April shows NULL growth in the second result: growth from zero is undefined, which is the honest answer.

In production

  • Keep a calendar dimension (one row per day with month, quarter and fiscal attributes) and join to it instead of generating the spine in every query.
  • Decide how the KPI displays growth from zero ("new", "n/a") rather than showing an infinite percentage.
  • Compare complete months only. The current, partial month makes growth look like a collapse.

Common mistakes

  • Integer division: (200 - 100) / 100 is 1, but (150 - 100) / 100 is 0 in integer arithmetic.
  • Using LAG without a spine when months can be missing.
  • Computing growth on raw rows before aggregating to months.

More on this topic in the SQL Interview Questions guide, or browse all SQL questions.