Pull the previous value in order.
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