← Learning Hub
Advanced

Window Functions

Window functions calculate across a group of rows but, unlike GROUP BY, keep every row. They power rankings, running totals and "compare with previous" reports - and are a favourite interview topic.

Window functions calculate across related rows while keeping every row in the result: rank products within each category, show a running total of sales, compare each month with the previous one. They're a favourite in data analyst interviews.

Ranking

Example
SELECT name, category, price,
       RANK() OVER (PARTITION BY category ORDER BY price DESC) AS rank_in_category
FROM products
ORDER BY category, rank_in_category;

-- Result:
-- name               | category    | price | rank_in_category
-- -------------------+-------------+-------+-----------------
-- Python Book        | Books       | 599   | 1
-- Notebook Pack      | Books       | 199   | 2
-- Smart Watch        | Electronics | 3999  | 1
-- Wireless Earbuds   | Electronics | 1499  | 2
-- Denim Jeans        | Fashion     | 1299  | 1
-- Cotton T-shirt     | Fashion     | 499   | 2
-- Table Lamp         | Home        | 899   | 1
-- Steel Water Bottle | Home        | 349   | 2
-- Yoga Mat           | Sports      | 799   | 1
▶ Try it yourself
  • OVER (...) makes it a window function.
  • PARTITION BY - restart for each group (like GROUP BY, but rows stay).
  • ROW_NUMBER() - 1, 2, 3...; RANK() - ties share a rank and leave a gap (1, 1, 3); DENSE_RANK() - no gap (1, 1, 2).

Top 1 per group

Example
SELECT category, name, price
FROM (SELECT category, name, price,
             ROW_NUMBER() OVER (PARTITION BY category ORDER BY price DESC) AS rn
      FROM products)
WHERE rn = 1;

-- Result:
-- category    | name        | price
-- ------------+-------------+------
-- Books       | Python Book | 599
-- Electronics | Smart Watch | 3999
-- Fashion     | Denim Jeans | 1299
-- Home        | Table Lamp  | 899
-- Sports      | Yoga Mat    | 799
▶ Try it yourself

Running total and previous value

Example
SELECT o.order_date,
       SUM(oi.qty * oi.price) AS day_sales,
       SUM(SUM(oi.qty * oi.price)) OVER (ORDER BY o.order_date) AS running_total,
       LAG(SUM(oi.qty * oi.price)) OVER (ORDER BY o.order_date) AS previous_day
FROM orders o JOIN order_items oi ON oi.order_id = o.id
WHERE o.status <> 'cancelled'
GROUP BY o.order_date
ORDER BY o.order_date;

-- Result:
-- order_date | day_sales | running_total | previous_day
-- -----------+-----------+---------------+-------------
-- 2026-10-02 | 1595      | 1595          | NULL
-- 2026-10-05 | 1499      | 3094          | 1595
-- 2026-10-18 | 1997      | 5091          | 1499
-- 2026-11-01 | 2097      | 7188          | 1997
-- 2026-11-03 | 848       | 8036          | 2097
-- 2026-11-10 | 2798      | 10834         | 848
-- 2026-11-15 | 599       | 11433         | 2798
▶ Try it yourself
LAG() looks at the previous row and LEAD() at the next. They make "this month vs last month" queries easy.
Key points
  • Window functions use OVER (PARTITION BY ... ORDER BY ...).
  • ROW_NUMBER, RANK and DENSE_RANK rank rows.
  • SUM() OVER gives running totals; LAG/LEAD look at neighbouring rows.

📝 Test yourself

1. Which function gives 1, 1, 2 for a tie at the top?
2. What does PARTITION BY category do?
3. Fill in the function that reads the previous row's value:
(total) OVER (ORDER BY day)