Year-over-Year (YoY), Month-over-Month (MoM) & Day-over-Day Growth
Compute growth rates against prior periods with LAG over time partitions.
In plain words
Put this period's number next to last period's number, then work out how much it grew in percent.
Think of it like…
A school report card that shows this term's marks next to last term's, with the change: you need both numbers for the same subject and the same kind of exam before the difference means anything.
Where you’ve seen it
Monthly revenue with MoM and YoY growth: aggregate orders per month, then LAG(revenue) OVER (ORDER BY month) for last month and LAG(revenue, 12) OVER (ORDER BY month) for the same month last year, provided every month is present.
Words you’ll hear
- MoM / YoY / DoD
- Compared with the previous month / the same period last year / the previous day.
- Growth rate
- (current − previous) / previous, usually shown as a percentage.
- Seasonality
- Regular yearly patterns (festive season, holidays), which is why YoY is often more meaningful than MoM.
- Period-to-date
- A partial current period (month to date) that must be compared with the same partial period before.
Visual diagram
Theory & example queries
The standard query
First aggregate to the period grain with date_trunc('month', created_at). Then LAG(revenue) OVER (ORDER BY month) gives last month's revenue on each row, and the growth rate is (revenue − prev) / NULLIF(prev, 0). Cast to numeric to avoid integer division, and multiply by 100 if you want a percentage.
For YoY on monthly data, LAG(revenue, 12) works only when the series has exactly one row for every month. A safer approach is a self join on the period shifted by a year (ON prev.month = cur.month - interval '1 year'), which is correct even when months are missing.
| id | customer_id | status | amount | coupon_code |
|---|---|---|---|---|
| 101 | 1 | paid | 249 | NULL |
| 102 | 2 | paid | 120 | WELCOME10 |
| 103 | 1 | shipped | 89.5 | NULL |
WITH m AS ( SELECT date_trunc('month', created_at) AS MONTH, SUM(amount) AS revenue FROM orders GROUP BY 1 )SELECT MONTH, revenue, LAG(revenue) OVER ( ORDER BY MONTH ) AS prev, ROUND( 100.0 * ( revenue - LAG(revenue) OVER ( ORDER BY MONTH ) ) / NULLIF( LAG(revenue) OVER ( ORDER BY MONTH ), 0 ), 1 ) AS mom_pctFROM m;PostgreSQL is the lesson baseline unless noted otherwise.
Read this line together with the surrounding statement as part of the same SQL expression.
Month-over-month growth3 sample rows
| month | revenue | prev | mom_pct |
|---|---|---|---|
| 2026-09-01 | 900000 | NULL | NULL |
| 2026-10-01 | 990000 | 900000 | 10 |
| 2026-11-01 | 1089000 | 990000 | 10 |
Note: Illustrative monthly revenue; the first month has no prior row, so prev and mom_pct are NULL.
Key takeaway: One row per period, LAG or a shifted join, and a NULLIF-protected division.
Making the comparison fair
On 19 November, "November so far" covers 19 days and October covers 31, so a naive MoM shows a big drop. Compare month-to-date with the same number of days in the previous month, or only compare complete periods.
MoM is distorted by seasonality (a festive-season month will always beat the month before), which is why business reviews lean on YoY. Also make sure both periods are measured the same way, with the same timezone, the same definition of a paid order, and refunds handled consistently.
SELECT cur.month, cur.revenue, prev.revenue AS last_year, (cur.revenue - prev.revenue) / NULLIF(prev.revenue, 0) AS yoyFROM m cur LEFT JOIN m prev ON prev.month = cur.month - interval '1 year';PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Year-over-year comparison3 sample rows
| month | revenue | last_year | yoy |
|---|---|---|---|
| 2025-11-01 | 900000 | NULL | NULL |
| 2026-10-01 | 1089000 | 880000 | 0.2375 |
| 2026-11-01 | 1125000 | 900000 | 0.25 |
Note: Assumes m contains monthly totals; values are rounded here only for readability, and missing prior-year rows produce NULL.
Key takeaway: Compare like with like: complete or equally partial periods, same definitions.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Aggregate to the period
Group by date_trunc('day' | 'month' | 'year', ts) to get exactly one row per period (per dimension if needed).
date_trunc('month', created_at)
When to use it & trade-offs
What it is
Period-over-period growth compares a metric with the previous period: day over day (DoD), month over month (MoM) or year over year (YoY). Aggregate to one row per period, fetch the previous period's value with LAG() (or a join on the shifted date), then compute (current − previous) / previous.
Why it matters: "Revenue is up 12% month over month" is how most business updates start, and every analytics interview includes a version of it. The details are where people go wrong: missing months break LAG, partial months compare unfairly, and dividing by zero or NULL crashes the query.
When to reach for it
- Monthly business reviews and KPI dashboards
- Tracking growth per product, region or channel
- Spotting anomalies with day-over-day changes
When to avoid or be careful
- LAG(…, 12) for YoY on a series with missing months
- Comparing a partial current month with a full previous month
- Integer division or unprotected division by a zero previous value
LAG vs shifted self join vs calendar table
Strengths: Short and fast.
Watch out: Wrong if any period is missing.
Strengths: Always compares the right calendar period.
Watch out: A little more code.
Strengths: Gap-free series with explicit zeros.
Watch out: Requires a calendar table or generate_series.
Interview signals & numbers to know
When this topic comes up
- “Month-over-month growth”
- “Compare with the same month last year”
- Growth percentage
- Seasonality
- Month-to-date
Benchmarks & rules of thumb
- (cur − prev) / prev
- growth rate
- 12
- LAG offset for YoY on monthly data (gap-free only)
- NULLIF(prev, 0)
- divide-by-zero guard
Common mistakes & follow-ups
Common interview mistakes
- Running LAG on raw orders instead of on one row per month.
- Using LAG(revenue, 12) when a month is missing, so "last year" is actually 13 months ago.
- Showing a 40% MoM drop on the 10th of the month because the current month is only a third complete.
Likely interviewer follow-ups
- How would you compute YoY growth if some months have no data?
- How would you compare month-to-date revenue fairly with last month?
- How would you compute MoM growth separately for each product category?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Year-over-Year (YoY), Month-over-Month (MoM) & Day-over-Day Growth with hands-on SQL practice and interview Q&A.