Growth Rates with LAG
Year-over-year growth is a staple of business reporting. LAG puts last year's number next to this year's, and the rest is arithmetic.
This lesson uses yearly_revenue from the E-commerce database.
Put each year next to the one before it:
Growth % = (this year - last year) / last year × 100:
The first year has no previous row, so LAG returns NULL and its growth is NULL too. That's correct: there's nothing to compare it with.
Writing LAG twice works, but a CTE is easier to read:
Check your understanding
Which formula gives year-over-year growth as a percent?
Examples run on the employees sample database. Open any of them in the playground to experiment.