SQL Window Functions
Chapter 28 of 40 · Estimated read time: 10 min
Window functions perform calculations across a set of rows related to the current row — without collapsing those rows the way GROUP BY does. Each row keeps its own output line; the window function adds an extra column showing the computed value for that row's "window" of data.
GROUP BY aggregate
Collapses rows — 7 order rows become 3 category rows. Original row detail is gone.
SELECT category, SUM(price) FROM products GROUP BY category; -- 3 rows returned
Window function
Keeps all rows — adds the aggregate as an extra column alongside each row's own data.
SELECT category, price, SUM(price) OVER (PARTITION BY category) FROM products; -- 7 rows returned
function_name() OVER (
PARTITION BY column -- optional: split rows into groups
ORDER BY column -- optional: define row order within window
ROWS/RANGE BETWEEN … -- optional: define the frame
)
Example 1 — Category Total Alongside Each Row
SELECT product_name, category, price, SUM(price) OVER (PARTITION BY category) AS category_total, ROUND(price * 100.0 / SUM(price) OVER (PARTITION BY category), 1) AS pct_of_category FROM products ORDER BY category, price;
Every row shows its own price AND the full category total — rows are not collapsed
| product_name | category | price | category_total | pct_of_category |
|---|---|---|---|---|
| HDMI Cable | Electronics | 299 | 2697 | 11.1 |
| Wireless Mouse | Electronics | 899 | 2697 | 33.3 |
| USB-C Hub | Electronics | 1499 | 2697 | 55.6 |
| Desk Lamp | Furniture | 1199 | 10198 | 11.8 |
| Ergonomic Chair | Furniture | 8999 | 10198 | 88.2 |
| Ballpoint Pens | Stationery | 49 | 148 | 33.1 |
| Notebook A5 | Stationery | 99 | 148 | 66.9 |
Example 2 — Running Total of Orders Over Time
SELECT order_id, order_date, total_amount, SUM(total_amount) OVER (ORDER BY order_date) AS running_total FROM orders ORDER BY order_date;
Running total grows with each order — no GROUP BY needed
| order_id | order_date | total_amount | running_total |
|---|---|---|---|
| 1001 | 2026-10-01 | 899 | 899 |
| 1002 | 2026-10-02 | 1499 | 2398 |
| 1003 | 2026-10-03 | 495 | 2893 |
| 1004 | 2026-10-04 | 8999 | 11892 |
| 1005 | 2026-10-05 | 2398 | 14290 |
| 1006 | 2026-10-06 | 299 | 14589 |
| 1007 | 2026-10-08 | 490 | 15079 |
ORDER BY inside OVER() defines the order of the running calculation. Without PARTITION BY, the window spans the entire result set.
Example 3 — ROW_NUMBER and RANK
SELECT product_name, category, price, ROW_NUMBER() OVER (PARTITION BY category ORDER BY price DESC) AS row_num, RANK() OVER (PARTITION BY category ORDER BY price DESC) AS price_rank FROM products ORDER BY category, price DESC;
ROW_NUMBER gives unique sequential numbers; RANK gives tied rows the same number
| product_name | category | price | row_num | price_rank |
|---|---|---|---|---|
| USB-C Hub | Electronics | 1499 | 1 | 1 |
| Wireless Mouse | Electronics | 899 | 2 | 2 |
| HDMI Cable | Electronics | 299 | 3 | 3 |
| Ergonomic Chair | Furniture | 8999 | 1 | 1 |
| Desk Lamp | Furniture | 1199 | 2 | 2 |
| Notebook A5 | Stationery | 99 | 1 | 1 |
| Ballpoint Pens | Stationery | 49 | 2 | 2 |
ROW_NUMBER vs RANK vs DENSE_RANK:
•
•
•
•
ROW_NUMBER — always unique: 1, 2, 3, 4•
RANK — ties get the same rank, next rank skips: 1, 1, 3, 4•
DENSE_RANK — ties get the same rank, no skipping: 1, 1, 2, 3Example 4 — LAG and LEAD (Access Previous / Next Row)
SELECT order_id, order_date, total_amount, LAG(total_amount) OVER (ORDER BY order_date) AS prev_order, LEAD(total_amount) OVER (ORDER BY order_date) AS next_order, total_amount - LAG(total_amount) OVER (ORDER BY order_date) AS change_from_prev FROM orders ORDER BY order_date;
LAG looks back one row; LEAD looks forward one row; NULL at the boundary
| order_id | order_date | total_amount | prev_order | next_order | change_from_prev |
|---|---|---|---|---|---|
| 1001 | 2026-10-01 | 899 | NULL | 1499 | NULL |
| 1002 | 2026-10-02 | 1499 | 899 | 495 | 600 |
| 1003 | 2026-10-03 | 495 | 1499 | 8999 | -1004 |
| 1004 | 2026-10-04 | 8999 | 495 | 2398 | 8504 |
| 1005 | 2026-10-05 | 2398 | 8999 | 299 | -6601 |
| 1006 | 2026-10-06 | 299 | 2398 | 490 | -2099 |
| 1007 | 2026-10-08 | 490 | 299 | NULL | 191 |
Example 5 — Top N Per Group (Classic Interview Question)
-- Top 1 most expensive product per category
WITH ranked AS (
SELECT
product_name,
category,
price,
ROW_NUMBER() OVER (PARTITION BY category ORDER BY price DESC) AS rn
FROM products
)
SELECT product_name, category, price
FROM ranked
WHERE rn = 1;Most expensive product in each category — a CTE + ROW_NUMBER pattern
| product_name | category | price |
|---|---|---|
| USB-C Hub | Electronics | 1499 |
| Ergonomic Chair | Furniture | 8999 |
| Notebook A5 | Stationery | 99 |
Top-N per group using ROW_NUMBER + CTE is one of the most common SQL interview questions. Memorise this pattern: PARTITION BY the group, ORDER BY the value, filter WHERE rn <= N.
✏️ Practice Exercises
1. Add a column showing the running total of total_amount per customer (PARTITION BY customer_id, ORDER BY order_date).
2. Rank customers by their total spend using RANK(). Who is #1?
3. Using LAG, find orders where total_amount dropped compared to the previous order.
Show Answers
-- 1.
SELECT
customer_id, order_id, order_date, total_amount,
SUM(total_amount) OVER (
PARTITION BY customer_id
ORDER BY order_date
) AS running_total_per_customer
FROM orders;
-- 2.
SELECT
customer_id,
SUM(total_amount) AS total_spent,
RANK() OVER (ORDER BY SUM(total_amount) DESC) AS spend_rank
FROM orders
GROUP BY customer_id;
-- 3.
SELECT order_id, order_date, total_amount,
LAG(total_amount) OVER (ORDER BY order_date) AS prev_amount
FROM orders
WHERE total_amount < LAG(total_amount) OVER (ORDER BY order_date);
-- Note: use a CTE or subquery to reference the window result in WHERE