Learn SQL/Advanced/Window Functions/SQL Year-over-Year Growth

SQL Year-over-Year Growth

The identical formula as Month-over-Month — just comparing against 12 periods back instead of 1.

What & Why

Year-over-year growth is the same LAG + percentage-change formula as month-over-month — the only difference is comparing each period to the same period one year earlier instead of the immediately preceding one.

See How It Works

BUSINESS QUESTION

Product compares feature releases in 2024 with the real 2023 release represented in product.features.

idnamedescriptionteamreleased_atdeprecated_at
1Activation ChecklistGuides new users through setupgrowth2024-01-20NULL
2CSV ExportExports report dataplatform2024-02-14NULL
3Invite NudgesPrompts workspace collaborationgrowth2024-03-05NULL
4Legacy DashboardOriginal reporting surfaceanalytics2023-08-102024-06-01
EXAMPLE QUERY
WITH yearly AS (
  SELECT EXTRACT(YEAR FROM released_at)::int AS release_year, COUNT(*) AS features_released
  FROM product.features
  WHERE released_at IS NOT NULL
  GROUP BY release_year
), compared AS (
  SELECT
    release_year,
    features_released,
    LAG(features_released) OVER (ORDER BY release_year) AS prior_year_releases
  FROM yearly
)
SELECT
  release_year,
  features_released,
  prior_year_releases,
  ROUND((features_released - prior_year_releases)::numeric / NULLIF(prior_year_releases, 0) * 100, 2) AS yoy_growth_pct
FROM compared
ORDER BY release_year;
RESULT — feature releases compared year over year
release_yearfeatures_releasedprior_year_releasesyoy_growth_pct
20231NULLNULL
202431200.00

The fixture contains one 2023 feature and three 2024 features, so the 2024 release count grew by 200%.

This lesson's practice is part of Pro.

Advanced business practice

Sign up free to try it on a real business scenario