Practice · Economy · Hard

Year-over-year GDP change (window function)

For the "GDP (current US$)" series, return each year, its value, and the change from the previous year (this year minus last year) as a column named yoy_change. Use a window function. The first year’s change will be NULL.

Runs in your browser. No signup needed.

What you are given

indicators(name, year, value). Use LAG(value) OVER (ORDER BY year). SQLite supports window functions.

Starting point

SELECT year, value,
       value - LAG(value) OVER (ORDER BY year) AS yoy_change
FROM indicators
WHERE name = 'GDP (current US$)'
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

Window functions (LAG, LEAD, ROW_NUMBER, running totals) are the backbone of analytics SQL — and they run right here in the browser via SQLite. (SQLite docs)

More Economy challenges

Open it in the playground →