SQLAdvanced#windows

What do LAG and LEAD do?

They read a value from a previous or following row in the same window, which makes period-over-period comparisons easy without a self join.

Example
SELECT month, revenue,
       revenue - LAG(revenue) OVER (ORDER BY month) AS growth
FROM monthly_sales;

Related Questions

1
SQLIntermediate#queries#windows

How do you find the Nth highest salary?

Open
2
SQLIntermediate#queries

How do you find and delete duplicate rows?

Open
3
SQLIntermediate#queries

How do you find employees earning more than their department average?

Open