Ranking and offsets
Window functions let you number positions, recognize ties, and compare a row with its neighbors.
ROW_NUMBER
ROW_NUMBER assigns a unique sequence within a partition. Define an ordering and tie-breaker if the chosen first row must be deterministic.
RANK and DENSE_RANK
RANK gives tied rows the same rank and leaves gaps afterward. DENSE_RANK shares ties without gaps. Adding a unique ID to the window order removes ties, which may defeat your intended ranking.
LAG and LEAD
LAG reads a preceding row; LEAD reads a following row according to the window order. Their default offset is one. At a boundary, the result is NULL unless you provide a default.
Comparisons and first rows
A change from the previous order can be sales - LAG(sales) OVER (...). The first row has no previous sale. Do not turn that missing comparison into zero unless the business definition supports it.