Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: Performance & internals
Stage 5: Specialization

Part 07 / Topic 1 of 8

OVER and PARTITION BY

GROUP BY answers "one number per group" by collapsing rows. A window function computes across a group of rows too — but every input row survives, each carrying the result alongside its own data:

SELECT name, category_id, price,
       round(avg(price) OVER (PARTITION BY category_id), 2) AS category_avg
FROM products
ORDER BY category_id, price DESC;
        name        | category_id |  price  | category_avg 
--------------------+-------------+---------+--------------
 USB-C Cable        |           1 |   15.00 |        15.00
 Postgres Deep Dive |           2 |   55.00 |        45.00
 SQL for Beginners  |           2 |   35.00 |        45.00
 Desk Lamp          |           3 |   45.00 |        45.00
 Laptop Pro 14      |           4 | 1200.00 |      1075.00
 Laptop Air 13      |           4 |  950.00 |      1075.00
 Phone X            |           5 |  799.00 |       599.00
 Phone Lite         |           5 |  399.00 |       599.00
 Coffee Maker       |           6 |  120.00 |       100.00
 Chef Knife         |           6 |   80.00 |       100.00
(10 rows)

Ten rows in, ten rows out — each product still itself, now with its category's average sitting next to it (ready for "how far off the average is this one?"). The grammar:

  • OVER (...) turns an aggregate into a window function.
  • PARTITION BY category_id splits rows into windows the way GROUP BY would split them into groups — but without the collapse.
  • Empty OVER () means one window over everything.

When you want just the per-group number, GROUP BY. When you want per-row values and the group's number together — the shape every comparison, ranking and running total needs — windows.

🧪 Quick quiz — answer from memory, then check.

  1. How do window functions differ from GROUP BY?
  2. What does an empty OVER () mean?
Show the answers
  1. They compute across related rows but keep every input row, one output per input — no collapsing.
  2. The window is the entire result set — one group containing everything.

Exercise 1: Rank within category

What to do: Every product with its price rank inside its own category, priciest = 1.

Sample output:

 category_id |        name        |  price  | rank_in_category 
-------------+--------------------+---------+------------------
           1 | USB-C Cable        |   15.00 |                1
           2 | Postgres Deep Dive |   55.00 |                1
           2 | SQL for Beginners  |   35.00 |                2
           3 | Desk Lamp          |   45.00 |                1
           4 | Laptop Pro 14      | 1200.00 |                1
           4 | Laptop Air 13      |  950.00 |                2
           5 | Phone X            |  799.00 |                1
           5 | Phone Lite         |  399.00 |                2
           6 | Coffee Maker       |  120.00 |                1
           6 | Chef Knife         |   80.00 |                2
(10 rows)

💡 Hint: RANK() OVER (PARTITION BY category_id ORDER BY price DESC).


Show the solution to Exercise 1
-- Exercise 1: Rank within category
SELECT category_id, name, price,
       RANK() OVER (PARTITION BY category_id ORDER BY price DESC) AS rank_in_category
FROM products
ORDER BY category_id, rank_in_category;

Search the guide