Practice · Demographics · Hard

Population growth per period

For each row in population_years, return year, population, and the change from the previous listed year as growth (this minus previous). Oldest first; the first row’s growth is NULL.

Runs in your browser. No signup needed.

What you are given

Use population − LAG(population) OVER (ORDER BY year). LAG reaches back one row.

Starting point

SELECT year, population,
       population - LAG(population) OVER (ORDER BY year) AS growth
FROM population_years
ORDER BY year;

The reference solution is checked automatically when you run your query on the practice page — results are compared row by row, and order matters for this one.

Did you know

LAG/LEAD let a row peek at its neighbours — the foundation of every “change since last period” metric. (SQLite docs)

More Demographics challenges

Open it in the playground →