AlgoMaster Logo

LAG, LEAD, and Row Comparison

Last Updated: May 3, 2026

7 min read

Ranking functions tell you where a row sits relative to others. But they don't let you compare a row's value to the row before it or after it. When you need to calculate the difference between today's revenue and yesterday's, or detect when a user changed their subscription tier, you need to reach across rows.

LAG and LEAD do exactly that: they pull a value from a previous or subsequent row into the current row, without a self-join.

Premium Content

Subscribe to unlock full access to this content and more premium articles.