Section: LAG and LEAD basics

Previous row with LAG

sql
sql
SELECT day,
       revenue,
       LAG(revenue) OVER (ORDER BY day) AS prev_revenue
FROM daily_revenue;
Explanation

`LAG` is the easiest way to compare the current row to the previous row.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing LAG and LEAD basics
Provide a LAG default value
Avoid NULL on the first row.
OpenIn sheetsqlsame section
Next row with LEAD
Preview the next value in sequence.
OpenIn sheetsqlsame section
Look back two rows
Custom offset with LAG.
OpenIn sheetsqlsame section
Day-over-day absolute change
Subtract the previous day's value.
Day-over-day percent change
Compute relative change versus previous row.
Detect gaps between events
Measure time since the prior event.