LAG(col, 1) over (partition by … order by ts) returns col from the previous row in the same partition; LEAD(col, 1) returns the next. Useful for diffs (sales - prev_sales), change detection, lookahead features.
daily_sales - LAG(daily_sales, 1) OVER (PARTITION BY sku ORDER BY day) gives daily change.
LEAD(ts, 1) returns the next event timestamp for time-to-next-event calculations.
Default value: LAG(col, 1, 0) -> 0 when no previous row exists.
partition by sku order by day:
day sales lag(sales)
d-1 100 - (default 0)
d 120 100
d+1 115 120
LAG/LEAD turn time-series into “compare with neighbor in O(1) SQL”. No self-join needed.