28. Window functions
Work out over a group of rows, as GROUP BY does, yet keep every row: rankings, shares of a total, the best of each group.
GROUP BY folds rows; OVER keeps them
Chapter 10's GROUP BY folds the rows of each group into one. Ask for the average price of each category and you get 4 rows, one per category; the products themselves are gone:
SELECT category_id, count(*) AS products, avg(price) AS avg_price
FROM product
GROUP BY category_id;| category_id | products | avg_price |
|---|---|---|
| 1 | 3 | 46.6666666666667 |
| 2 | 3 | 71.6666666666667 |
| 3 | 3 | 88.3333333333333 |
| 4 | 3 | 23.3333333333333 |
Often you want both at once: each product, and a figure worked out over many rows beside it, such as "how far is this price from the average?". That is what a window function does. Put OVER (...) after a function like avg() and it no longer folds rows: it works over a "window" of rows and writes the answer on every row.
OVER () with nothing inside means the window is the whole result:
SELECT name, price, avg(price) OVER () AS avg_all, price - avg(price) OVER () AS diff
FROM product
ORDER BY id;| name | price | avg_all | diff |
|---|---|---|---|
| Coffee | 55.0 | 57.5 | -2.5 |
| Green Tea | 45.0 | 57.5 | -12.5 |
| Orange Juice | 40.0 | 57.5 | -17.5 |
| Croissant | 35.0 | 57.5 | -22.5 |
| Banana Bread | 60.0 | 57.5 | 2.5 |
| Cheesecake | 120.0 | 57.5 | 62.5 |
| Potato Chips | 25.0 | 57.5 | -32.5 |
| Dried Mango | 90.0 | 57.5 | 32.5 |
| Cashew Nuts | 150.0 | 57.5 | 92.5 |
| Notebook | 30.0 | 57.5 | -27.5 |
| Pen | 15.0 | 57.5 | -42.5 |
| Highlighter | 25.0 | 57.5 | -32.5 |
All 12 products are still there. Each has the average of all 12, 57.5, beside it, and diff shows how far it is from the average: Cashew Nuts is 92.5 above it, Pen 42.5 below.
Every aggregate you know, count, sum, avg, min, max, works as a window function once it has OVER.
PARTITION BY: a window for each group
PARTITION BY splits the rows into groups, as GROUP BY does, but again keeps every row. Each row sees only the rows of its own group:
SELECT name, category_id, price,
count(*) OVER (PARTITION BY category_id) AS in_category,
avg(price) OVER (PARTITION BY category_id) AS category_avg
FROM product
ORDER BY category_id, id;| name | category_id | price | in_category | category_avg |
|---|---|---|---|---|
| Coffee | 1 | 55.0 | 3 | 46.6666666666667 |
| Green Tea | 1 | 45.0 | 3 | 46.6666666666667 |
| Orange Juice | 1 | 40.0 | 3 | 46.6666666666667 |
| Croissant | 2 | 35.0 | 3 | 71.6666666666667 |
| Banana Bread | 2 | 60.0 | 3 | 71.6666666666667 |
| Cheesecake | 2 | 120.0 | 3 | 71.6666666666667 |
| Potato Chips | 3 | 25.0 | 3 | 88.3333333333333 |
| Dried Mango | 3 | 90.0 | 3 | 88.3333333333333 |
| Cashew Nuts | 3 | 150.0 | 3 | 88.3333333333333 |
| Notebook | 4 | 30.0 | 3 | 23.3333333333333 |
| Pen | 4 | 15.0 | 3 | 23.3333333333333 |
| Highlighter | 4 | 25.0 | 3 | 23.3333333333333 |
Coffee, Green Tea and Orange Juice (category 1) each see the 3 drinks, with their average of about 46.67; Croissant, Banana Bread and Cheesecake see the bakery's average of about 71.67, and so on. These are the same numbers as the GROUP BY above, now written beside each product.
The ORDER BY at the end of the statement only sorts what comes out. It has nothing to do with the windows: a window decides which rows a function sees, not the order the result is shown in.
ORDER BY inside OVER: numbering and ranking
Put ORDER BY inside OVER and the rows of the window are lined up in that order. Now functions that number rows make sense. row_number() gives 1, 2, 3, ... in that order:
SELECT name, price, row_number() OVER (ORDER BY price DESC, name) AS place
FROM product
ORDER BY place;| name | price | place |
|---|---|---|
| Cashew Nuts | 150.0 | 1 |
| Cheesecake | 120.0 | 2 |
| Dried Mango | 90.0 | 3 |
| Banana Bread | 60.0 | 4 |
| Coffee | 55.0 | 5 |
| Green Tea | 45.0 | 6 |
| Orange Juice | 40.0 | 7 |
| Croissant | 35.0 | 8 |
| Notebook | 30.0 | 9 |
| Highlighter | 25.0 | 10 |
| Potato Chips | 25.0 | 11 |
| Pen | 15.0 | 12 |
Cashew Nuts, the dearest, is 1; Pen, the cheapest, is 12.
Highlighter and Potato Chips cost the same, 25. If the ORDER BY inside OVER were only price DESC, which of them came first would be left to chance, and could change one day. That is why name is there too, to settle ties. When you number rows, always order by something that cannot tie (add id or name at the end).
When ties should share a place, as in a contest, there are three ways to number. See them side by side:
SELECT name, price,
row_number() OVER (ORDER BY price, name) AS row_number,
rank() OVER (ORDER BY price) AS rank,
dense_rank() OVER (ORDER BY price) AS dense_rank
FROM product
ORDER BY price, name;| name | price | row_number | rank | dense_rank |
|---|---|---|---|---|
| Pen | 15.0 | 1 | 1 | 1 |
| Highlighter | 25.0 | 2 | 2 | 2 |
| Potato Chips | 25.0 | 3 | 2 | 2 |
| Notebook | 30.0 | 4 | 4 | 3 |
| Croissant | 35.0 | 5 | 5 | 4 |
| Orange Juice | 40.0 | 6 | 6 | 5 |
| Green Tea | 45.0 | 7 | 7 | 6 |
| Coffee | 55.0 | 8 | 8 | 7 |
| Banana Bread | 60.0 | 9 | 9 | 8 |
| Dried Mango | 90.0 | 10 | 10 | 9 |
| Cheesecake | 120.0 | 11 | 11 | 10 |
| Cashew Nuts | 150.0 | 12 | 12 | 11 |
row_number()never repeats: Highlighter is 2, Potato Chips 3, even though they cost the same.rank()gives ties the same place, then skips: both are 2, and Notebook after them is 4, as in a race where two people share second place and nobody comes third.dense_rank()gives ties the same place but doesn't skip: both are 2, and Notebook is 3. The last product'sdense_rankis 11, because there are 11 different prices.
ntile(n) shares the rows out into n groups as evenly as it can, in order. ntile(4) splits the products into quarters by price:
SELECT name, price, ntile(4) OVER (ORDER BY price, name) AS quarter
FROM product
ORDER BY price, name;| name | price | quarter |
|---|---|---|
| Pen | 15.0 | 1 |
| Highlighter | 25.0 | 1 |
| Potato Chips | 25.0 | 1 |
| Notebook | 30.0 | 2 |
| Croissant | 35.0 | 2 |
| Orange Juice | 40.0 | 2 |
| Green Tea | 45.0 | 3 |
| Coffee | 55.0 | 3 |
| Banana Bread | 60.0 | 3 |
| Dried Mango | 90.0 | 4 |
| Cheesecake | 120.0 | 4 |
| Cashew Nuts | 150.0 | 4 |
12 products in 4 groups make 3 to a group: quarter 1 is the cheapest three, quarter 4 the dearest three.
PARTITION BY and ORDER BY can go together in the same OVER: numbering starts again at 1 in each group. Here is each product's place within its own category, by price:
SELECT category_id, name, price,
row_number() OVER (PARTITION BY category_id ORDER BY price DESC) AS place_in_category
FROM product
ORDER BY category_id, place_in_category;| category_id | name | price | place_in_category |
|---|---|---|---|
| 1 | Coffee | 55.0 | 1 |
| 1 | Green Tea | 45.0 | 2 |
| 1 | Orange Juice | 40.0 | 3 |
| 2 | Cheesecake | 120.0 | 1 |
| 2 | Banana Bread | 60.0 | 2 |
| 2 | Croissant | 35.0 | 3 |
| 3 | Cashew Nuts | 150.0 | 1 |
| 3 | Dried Mango | 90.0 | 2 |
| 3 | Potato Chips | 25.0 | 3 |
| 4 | Notebook | 30.0 | 1 |
| 4 | Highlighter | 25.0 | 2 |
| 4 | Pen | 15.0 | 3 |
Windows are worked out after WHERE and GROUP BY
A statement works in this order: FROM, then WHERE, then GROUP BY and HAVING, and only then the window functions in SELECT, and last ORDER BY. So WHERE cannot use a window function: when WHERE runs, the window has not been worked out yet:
SELECT name, price
FROM product
WHERE row_number() OVER (ORDER BY price DESC) <= 3;The way out is a CTE (chapter 26): work out the numbers in one step, then pick rows from it in the next:
WITH numbered AS (
SELECT name, price, row_number() OVER (ORDER BY price DESC, name) AS place
FROM product
)
SELECT name, price, place FROM numbered
WHERE place <= 3
ORDER BY place;| name | price | place |
|---|---|---|
| Cashew Nuts | 150.0 | 1 |
| Cheesecake | 120.0 | 2 |
| Dried Mango | 90.0 | 3 |
Coming after GROUP BY has a good side, though: a window function can rank groups that GROUP BY has just made. Here each order's total is ranked from the biggest down:
SELECT o.id, o.ordered, sum(oi.qty * oi.price) AS total,
rank() OVER (ORDER BY sum(oi.qty * oi.price) DESC) AS place
FROM orders AS o JOIN order_item AS oi ON oi.order_id = o.id
GROUP BY o.id
ORDER BY place, o.id;| id | ordered | total | place |
|---|---|---|---|
| 9 | 2026-10-22 | 490.0 | 1 |
| 8 | 2026-10-15 | 390.0 | 2 |
| 15 | 2026-11-25 | 375.0 | 3 |
| 4 | 2026-09-20 | 300.0 | 4 |
| 11 | 2026-11-02 | 300.0 | 4 |
| 6 | 2026-10-03 | 290.0 | 6 |
| 7 | 2026-10-10 | 235.0 | 7 |
| 1 | 2026-09-02 | 180.0 | 8 |
| 3 | 2026-09-12 | 175.0 | 9 |
| 12 | 2026-11-05 | 175.0 | 9 |
| 2 | 2026-09-05 | 165.0 | 11 |
| 5 | 2026-10-01 | 150.0 | 12 |
| 13 | 2026-11-11 | 90.0 | 13 |
| 14 | 2026-11-18 | 80.0 | 14 |
| 10 | 2026-10-28 | 45.0 | 15 |
Order 9 is the biggest, 490 baht. Orders 4 and 11 both come to 300 and share 4th place, so the next is 6th; orders 3 and 12 share 9th at 175, and the next is 11th. rank() does exactly what a contest does.
The best seller of each category
"The best one of each group" (the top seller of each category, the latest order of each customer) is one of the commonest questions in real work, and window functions answer it neatly. First, how many of each product sold:
SELECT p.category_id, p.name, sum(oi.qty) AS sold
FROM product AS p JOIN order_item AS oi ON oi.product_id = p.id
GROUP BY p.id
ORDER BY p.category_id, sold DESC;| category_id | name | sold |
|---|---|---|
| 1 | Coffee | 7 |
| 1 | Green Tea | 5 |
| 1 | Orange Juice | 2 |
| 2 | Croissant | 6 |
| 2 | Banana Bread | 3 |
| 2 | Cheesecake | 3 |
| 3 | Potato Chips | 11 |
| 3 | Dried Mango | 4 |
| 3 | Cashew Nuts | 2 |
| 4 | Pen | 35 |
| 4 | Notebook | 18 |
11 rows: Highlighter has never sold, so the JOIN leaves it out. Now three steps in a CTE: add up sales, rank them within each category, keep those ranked 1:
WITH sales AS (
SELECT p.category_id, p.name, sum(oi.qty) AS sold
FROM product AS p JOIN order_item AS oi ON oi.product_id = p.id
GROUP BY p.id
),
ranked AS (
SELECT category_id, name, sold,
rank() OVER (PARTITION BY category_id ORDER BY sold DESC) AS place
FROM sales
)
SELECT c.name AS category, r.name AS best_seller, r.sold
FROM ranked AS r JOIN category AS c ON c.id = r.category_id
WHERE r.place = 1
ORDER BY c.id;| category | best_seller | sold |
|---|---|---|
| Drinks | Coffee | 7 |
| Bakery | Croissant | 6 |
| Snacks | Potato Chips | 11 |
| Stationery | Pen | 35 |
Coffee leads the drinks with 7, Croissant the bakery with 6, Potato Chips the snacks with 11, and Pen the stationery with 35.
Choose the numbering by what you want when there is a tie. rank() (or dense_rank()) with place = 1 gives every product that shares first place, so a category might show two rows. row_number() gives exactly one row per category, but which of the tied ones is left to your ORDER BY. For "the top 3 of each group" use place <= 3 the same way.
- Give every customer a member number, 1, 2, 3, ... in the order they joined (
joined). - Show each order's total and its place among all orders, the biggest first, with tied orders sharing a place.
- Show the two cheapest products of each category (if prices tie, the name decides).
Show the answer
SELECT name, joined, row_number() OVER (ORDER BY joined, id) AS member_no
FROM customer
ORDER BY member_no;
SELECT o.id, sum(oi.qty * oi.price) AS total,
rank() OVER (ORDER BY sum(oi.qty * oi.price) DESC) AS place
FROM orders AS o JOIN order_item AS oi ON oi.order_id = o.id
GROUP BY o.id
ORDER BY place, o.id;
WITH ranked AS (
SELECT category_id, name, price,
row_number() OVER (PARTITION BY category_id ORDER BY price, name) AS place
FROM product
)
SELECT category_id, name, price FROM ranked
WHERE place <= 2
ORDER BY category_id, place;| name | joined | member_no |
|---|---|---|
| Somchai Jaidee | 2025-01-15 | 1 |
| Malee Srisuk | 2025-03-02 | 2 |
| Anan Wongsa | 2025-04-20 | 3 |
| Pim Chaiya | 2025-06-11 | 4 |
| Narin Thongdee | 2025-08-30 | 5 |
| Kanya Boonmee | 2025-10-05 | 6 |
| Wichai Saelim | 2026-01-09 | 7 |
| Siriporn Kaewdee | 2026-02-14 | 8 |
| Tawan Rungruang | 2026-05-21 | 9 |
| Lalita Meesuk | 2026-08-03 | 10 |
| id | total | place |
|---|---|---|
| 9 | 490.0 | 1 |
| 8 | 390.0 | 2 |
| 15 | 375.0 | 3 |
| 4 | 300.0 | 4 |
| 11 | 300.0 | 4 |
| 6 | 290.0 | 6 |
| 7 | 235.0 | 7 |
| 1 | 180.0 | 8 |
| 3 | 175.0 | 9 |
| 12 | 175.0 | 9 |
| 2 | 165.0 | 11 |
| 5 | 150.0 | 12 |
| 13 | 90.0 | 13 |
| 14 | 80.0 | 14 |
| 10 | 45.0 | 15 |
| category_id | name | price |
|---|---|---|
| 1 | Orange Juice | 40.0 |
| 1 | Green Tea | 45.0 |
| 2 | Croissant | 35.0 |
| 2 | Banana Bread | 60.0 |
| 3 | Potato Chips | 25.0 |
| 3 | Dried Mango | 90.0 |
| 4 | Pen | 15.0 |
| 4 | Highlighter | 25.0 |
Phai (bamboo), given as a gift of knowledge: free to use, learn from and pass on · every example on this page was run on Phai SQL and checked against SQLite when the page was made