Problem Statement
What does LAG(value, 1, 0) return when there is no previous row in the partition?
Explanation
LAG reads a value from a previous row in the same ordered partition. When that row does not exist, the default is used.
Supplying a safe default keeps arithmetic stable and avoids NULL propagation in first-row calculations.
Code Solution
SolutionRead Only
SELECT day, amount,
LAG(amount, 1, 0) OVER (PARTITION BY product ORDER BY day) AS prev_amt
FROM daily_sales;