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.

1 min čteníSQL · LAG · LEAD · window · tutorial

LAG and LEAD are window functions that look at neighbouring rows. LAG returns the previous row's value, LEAD the 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.

Procvičujte na reálných úlohách

Řešte úlohy v SQL trenéru s okamžitým hodnocením a nápovědami.

Otevřít trenéra