Problem Statement
What do LAG and LEAD let you do without a self join?
Explanation
LAG peeks backward and LEAD peeks forward along the ordered rows in the same partition. This is perfect for day-over-day changes or trend detection.
Because they run inside the same result set, you avoid extra joins and keep the query simpler and faster.
Code Solution
SolutionRead Only
SELECT day, amount,
amount - LAG(amount) OVER (ORDER BY day) AS delta
FROM sales_by_day;