LAG
Takes a value from the previous row to compare with the current one.
Syntax
LAG(<expression>) OVER (ORDER BY <sort key>)
LAG(<expression>, <offset>, <default>) OVER (ORDER BY <sort key>)
LAG(<expression>) OVER (PARTITION BY <group> ORDER BY <sort key>)
Looks at the row above in the given order and returns a value from it. The offset is how many rows back (1 by default), and the third argument is used when there's no previous row. Its mirror function, LEAD, takes a value from the next row.