SQLLAGLEADwindow
What are LAG and LEAD in SQL? Neighbouring rows in a window for beginners
LAG and LEAD return the value from the **previous** or **next** row of a window. Plain words: day-over-day delta, time-to-next-event, price change — tasks that previously required self-joining a table. With tables and common pitfalls.
PreviousWhat is PARTITION BY in SQL? Groups inside a window for beginnersNextSQL Window Frames: ROWS/RANGE, Running Totals, and the LAST_VALUE Trap
Related articles
Tasks on this topic
Purchase sequence number per userTime Gaps Between Track PlaysOnline Store Session Start FlagNeobank Operating Cash Balance
All problem sets →
LAGandLEADare window functions that look at neighbouring rows.LAGreturns the previous row's value,LEADthe next. They unlock day-over-day deltas, time-to-next-event, price changes — all the things that previously required self-joins.This article is currently in Russian. The full English translation is on the way.