0
← Exercises/
Window Functions•1 of 14
Window FunctionsIntermediate

Month-over-Month Revenue Growth with LAG()

Scenario

Finance and executive leadership evaluate SaaS health based on Month-over-Month (MoM) revenue acceleration. Using SQL window functions allows calculating historical velocity without costly self-joins.

Task
Analyze monthly invoiced revenue in `monthly_revenue`. For each month, compute: 1. `month_date`: In date format (YYYY-MM-DD) 2. `revenue`: The current month's revenue 3. `prev_month_revenue`: The preceding month's revenue using `LAG()` 4. `growth_pct`: Percentage increase from previous month rounded to 2 decimal places: `ROUND(100.0 * (revenue - LAG(revenue) OVER (ORDER BY month_date)) / LAG(revenue) OVER (ORDER BY month_date), 2)` Order results chronologically by `month_date` ascending. Note that the first month will have `NULL` for previous revenue and growth.
Expected Output Format

Time-series table showing month_date, revenue, prev_month_revenue, and growth_pct.

Input Tables & Sample Data0 tables
Loading database schema...
Need a hint?
query.sql|Local Sandbox
Loading SQL editor...
Click "Run" or press ⌘ + Enter to execute your query

About the “Month-over-Month Revenue Growth with LAG()” SQL Problem

Business Scenario

Finance and executive leadership evaluate SaaS health based on Month-over-Month (MoM) revenue acceleration. Using SQL window functions allows calculating historical velocity without costly self-joins.

Expected Output & Criteria

Time-series table showing month_date, revenue, prev_month_revenue, and growth_pct.

Database Table Schema (PostgreSQL DDL)

PostgreSQL 16 WASM

This challenge executes directly against the following relational schema in your browser:

CREATE TABLE monthly_revenue (
        month_date DATE PRIMARY KEY,
        revenue NUMERIC(12, 2) NOT NULL
      );

Target SQL Clauses & Operators

LAGOVERORDER BY

Want more Window Functions practice?

Explore the full syllabus of 14 exercises, syntax guides, and interview pitfalls.

View Window Functions Track→