Practice · Demographics · Hard
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.
Use population − LAG(population) OVER (ORDER BY year). LAG reaches back one row.
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.
LAG/LEAD let a row peek at its neighbours — the foundation of every “change since last period” metric. (SQLite docs)