Stop 08 · Window FunctionsProjects · 2/2

Month-over-Month Revenue Growth Analysis

Part of the SQL Roadmap

In short

"Are we growing?" is the question every founder asks. In this project you turn raw orders into a monthly table with last month's revenue and the growth percentage, the kind of table that goes straight into a board deck.

A project that calculates monthly revenue, the previous month's revenue with LAG, and the percentage change between them.

The jaadu holds two paper bills side by side at a doorstep, comparing a tall stack of coins next to each one with a curious frown.

Think of it like this

Reading your electricity bill

Your electricity bill in Ahmedabad shows this month's units right next to last month's, so you instantly see if usage went up. Your analysis does the same for revenue: put each month beside the one before it and work out the change in percent.

Watch it happen

From orders to growth percentages

Orders
Month bucket
Monthly sum
LAG
Growth %

1/5Thousands of individual orders, each with a date and an amount in rupees.

The big ideas

What you need to know

Aggregate first

LAG on raw orders compares order to order. Sum to one row per month in a CTE, then apply LAG.

Guard the division

Wrap the previous month in NULLIF(prev, 0) so a zero month gives NULL instead of a divide-by-zero error.

Fill missing months

A month with no orders has no row, so LAG would skip it. Join to generate_series of months to keep gaps visible.

Write the story

Numbers need words. Note the best month, the worst month and any festive-season pattern you see.

How it works

The analysis step by step

  1. 1

    Bucket by month

    Truncate each order date to the first of its month.

  2. 2

    Sum per month

    One row per month with total revenue.

  3. 3

    Bring in last month

    LAG(revenue) ordered by month.

  4. 4

    Compute growth

    Difference divided by last month, as a percentage, rounded to one decimal.

  5. 5

    Explain it

    Write two or three plain observations about the trend.

See it in code

Walk through the code

sql
1WITH monthly AS (2  SELECT3    DATE_TRUNC('month', order_date)::date AS month,4    SUM(amount) AS revenue5  FROM orders6  WHERE status = 'delivered'7  GROUP BY 18)9SELECT10  month,11  revenue,12  LAG(revenue) OVER (ORDER BY month) AS prev_revenue,13  ROUND(14    100.0 * (revenue - LAG(revenue) OVER (ORDER BY month))15      / NULLIF(LAG(revenue) OVER (ORDER BY month), 0),16    117  ) AS growth_pct18FROM monthly19ORDER BY month;

1/4The CTE squashes orders into one row per month.

Monthly revenue, the previous month and the growth percentage. DATE_TRUNC and ::date are PostgreSQL. In MySQL 8+, group by DATE_FORMAT(order_date, '%Y-%m-01'). In SQL Server, use DATEFROMPARTS(YEAR(order_date), MONTH(order_date), 1).

Save yourself the pain

Common mistakes

Oops

Applying LAG directly to the orders table.

Do this instead

That compares each order with the order before it. Aggregate to months first, then use LAG.

Oops

Dividing by the previous month without a guard.

Do this instead

A month with ₹0 revenue causes a divide-by-zero error. Use NULLIF(prev_revenue, 0).

Oops

Ignoring months with no orders at all.

Do this instead

LEFT JOIN your monthly totals to generate_series(start, end, interval '1 month') and COALESCE revenue to 0.

Your mission

Build the growth table

~60 min

Quick check

Why does the growth_pct for the first month come back as NULL?

Go deeper

  1. 1Aggregate to monthly revenue in a CTE first, then apply LAG.
  2. 2Handle the first month and divide-by-zero cases safely.
  3. 3Write two or three observations about the trend you find.

More projects at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge