sqlstreak

SQL how-to

How to calculate month-over-month growth in SQL

Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)

Short answer

Total per month, then use LAG(total) OVER (ORDER BY month) to put the previous month on the same row. Growth is 100.0 * (total - previous) / previous. For year over year, compare with LAG(total, 12).

The task: Compare each month with the one before: the change and the percent change.

Minutes listened, month over month

WITH monthly AS (
  SELECT SUBSTR(CAST(played_on AS TEXT), 1, 7) AS month,
         SUM(minutes) AS minutes
  FROM plays
  GROUP BY 1
)
SELECT month, minutes,
       LAG(minutes) OVER (ORDER BY month) AS previous,
       ROUND(100.0 * (minutes - LAG(minutes) OVER (ORDER BY month))
             / LAG(minutes) OVER (ORDER BY month), 1) AS growth_pct
FROM monthly
ORDER BY month;

Result (PostgreSQL and SQLite, identical): 4 rows

monthminutespreviousgrowth_pct
2026-0112626NULLNULL
2026-022923012626131.5
2026-03483152923065.3
2026-042219548315-54.1

Try it: PostgreSQL playground · SQLite playground

How it works

  1. 1
    WITH monthly AS (…)

    First, one row per month with its total.

  2. 2
    LAG(minutes) OVER (ORDER BY month)

    The previous month's total on the current row; NULL for the first month.

  3. 3
    100.0 * (minutes - previous) / previous

    The percent change. 100.0 keeps it decimal; the first month is NULL because there's nothing to compare.

More examples

Growth per group

Sign-ups per source and month, compared with the same source's previous month.

WITH monthly AS (
  SELECT source, SUBSTR(CAST(signed_up_on AS TEXT), 1, 7) AS month, COUNT(*) AS signups
  FROM users
  GROUP BY source, 2
)
SELECT source, month, signups,
       signups - LAG(signups) OVER (PARTITION BY source ORDER BY month) AS change
FROM monthly
ORDER BY source, month;

Result (PostgreSQL and SQLite, identical): 12 rows

sourcemonthsignupschange
ad2026-0117NULL
ad2026-0212-5
ad2026-032816
friend2026-0117NULL
friend2026-023720
friend2026-03370
podcast2026-0114NULL
podcast2026-02239
podcast2026-0318-5
search2026-0132NULL
search2026-0228-4
search2026-03379

Try it: PostgreSQL playground · SQLite playground

Variations

Year over year

With several years of data, LAG(minutes, 12) OVER (ORDER BY month) reaches back twelve months, as long as no month is missing. Fill gaps with a generated calendar first.

Avoid dividing by zero

If a month can be 0, wrap the divisor: NULLIF(LAG(minutes) OVER (…), 0).

Common mistakes

  • Missing months: LAG takes the previous row, not the previous calendar month.

  • Integer division: 100 * (a - b) / b with whole numbers drops the decimals.

  • Comparing a partial current month with full months: the last month looks like a drop.

PostgreSQL and SQLite

Everything on this page works the same in PostgreSQL and SQLite: each example above was run on both, and the results match.

Official documentation

Go deeper

Make it stick in five minutes a day

“Active users and growth” teaches this step by step on a real database, with instant feedback. Part of sqlstreak Pro; the first lessons are free.

Start free