How do you calculate month-over-month and year-over-year growth in SQL with window functions?

Quick Overview

Asks how to compute month-over-month and year-over-year growth in SQL with window functions such as LAG. Tests aggregation to one row per period, the growth formula, division by zero and integer division, and handling of months that have no data.

How do you calculate month-over-month and year-over-year growth in SQL with window functions?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

How would you calculate period-over-period comparisons such as Month-over-Month (MoM) or Year-over-Year (YoY) growth in SQL, using window functions? Assume a transactions table such as `sales(dt, amount)` with one row per sale. ```hint Compare like with like Decide what one row should represent before any window function runs. ``` ```hint Watch the offset Think about what a row-based offset returns when a month in the middle of the series has no sales at all. ``` ### Clarifying Questions to Ask - Are periods calendar months, and in which time zone are dates truncated? - Should a month with no sales count as zero, or as missing data? - Is the current, partial month included in the output? - Is growth needed for the whole business or separately per segment, such as region or product? ### What a Strong Answer Covers - Aggregating raw rows to one row per period before comparing periods. - Using `LAG()` with the right ordering and offset for MoM and for YoY. - The growth formula, including division by zero and integer division. - Handling months that have no rows, so the offset still points at the intended prior period. - A complete query, with the output ordered by period. ### Follow-up Questions - How would you compute MoM growth separately for each region in the same query? - The current month is only half over. How do you keep it from showing a misleading drop? - How would you compute YoY with a self-join instead of `LAG()`, and when might that be the safer choice? - How would you report week-over-week growth on a 7-day rolling average instead of raw daily totals?

Overview: Asks how to compute month-over-month and year-over-year growth in SQL with window functions such as LAG. Tests aggregation to one row per period, the growth formula, division by zero and integer division, and handling of months that have no data.

PracHub logo
General
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

How would you calculate period-over-period comparisons such as Month-over-Month (MoM) or Year-over-Year (YoY) growth in SQL, using window functions? Assume a transactions table such as sales(dt, amount) with one row per sale.

Clarifying Questions to Ask Guidance

  • Are periods calendar months, and in which time zone are dates truncated?
  • Should a month with no sales count as zero, or as missing data?
  • Is the current, partial month included in the output?
  • Is growth needed for the whole business or separately per segment, such as region or product?

What a Strong Answer Covers Guidance

  • Aggregating raw rows to one row per period before comparing periods.
  • Using LAG() with the right ordering and offset for MoM and for YoY.
  • The growth formula, including division by zero and integer division.
  • Handling months that have no rows, so the offset still points at the intended prior period.
  • A complete query, with the output ordered by period.

Follow-up Questions Guidance

  • How would you compute MoM growth separately for each region in the same query?
  • The current month is only half over. How do you keep it from showing a misleading drop?
  • How would you compute YoY with a self-join instead of LAG() , and when might that be the safer choice?
  • How would you report week-over-week growth on a 7-day rolling average instead of raw daily totals?