SQL · Pattern 10 — Window Functions
LAG()
Accesses the previous row.
Source data — Pattern 10: Window Functions
All queries run against the shared employees table (see schema.sql). This pattern introduces a sales table to demonstrate window functions including ranking, navigation, running totals, moving averages and window aggregates.


What it does
Accesses the previous row.
Problem
Display the previous month's sales.
SELECT *,
LAG(sales_amount) OVER(ORDER BY sale_month) AS result
FROM sales;
Result
