Practice · History · Hard
Return name, discovery_year and years_since_previous — the gap to the previous discovery in chronological order — earliest first. The first row has no previous find.
Runs in your browser. No signup needed.
discoveries(name, discovery_year). Use LAG() to reach the previous row.
SELECT name, discovery_year,
discovery_year - LAG(discovery_year) OVER (ORDER BY ...) AS years_since_previous
FROM discoveries
ORDER BY ...;
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 returns NULL for the first row because there is nothing behind it — a reminder that window functions still respect the edges of the data.