Day 22 10 min
Window functions
1 / 12 · ≈ 8 min left
The problem
The sales lead wants a weekly report: each order with its rank, the running total of its region, and the change from the order before. A GROUP BY region gives one row per region and loses the orders. A window function computes over a group of rows and still returns every order.
The database is PostgreSQL. The table is orders (id int PRIMARY KEY, region text, day date, amount int) with 6 rows. In north: id 1 on 2026-10-01 for 100, id 2 on 2026-10-02 for 250, id 3 on 2026-10-03 for 250. In south: id 4 on 2026-10-01 for 80, id 5 on 2026-10-02 for 300, id 6 on 2026-10-03 for 120.