Sub topic 3
Splitting rows into partitions with PARTITION BY
This chapter assumes you can write GROUP BY queries with SUM, COUNT and AVG, and that you have seen the basic shape of an OVER (...) clause. The examples run on PostgreSQL, MySQL 8+, SQL Server, SQLite 3.25+ and Oracle. Oracle needs DATE '2026-01-05' literals in the setup script.
What a partition is
A window function looks at a set of rows related to the current row. PARTITION BY decides which rows belong to that set. The database divides the rows into groups that share the same value in the partition expressions. Each row then sees only the rows in its own group.
A partition is not a separate result row, unlike a GROUP BY group. Every input row stays in the output. The partition only controls what each row's window function can see.
Use this data for the examples in this chapter:
CREATE TABLE sales (
id INTEGER PRIMARY KEY,
region VARCHAR(10),
salesperson VARCHAR(20),
amount INTEGER,
sale_date DATE
);
INSERT INTO sales (id, region, salesperson, amount, sale_date) VALUES
(1, 'East', 'Ana', 100, '2026-01-05'),
(2, 'East', 'Ben', 250, '2026-01-12'),
(3, 'East', 'Ana', 150, '2026-02-03'),
(4, 'West', 'Cy', 400, '2026-01-20'),
(5, 'West', 'Dee', 200, '2026-02-11'),
(6, NULL, 'Eli', 50, '2026-02-15');Row 6 has no region on purpose. We come back to it below.
A first partitioned query
This query adds each region's total to every sale and computes each sale's share of that total:
SELECT
id,
region,
amount,
SUM(amount) OVER (PARTITION BY region) AS region_total,
ROUND(amount * 100.0 / SUM(amount) OVER (PARTITION BY region), 1) AS pct_of_region
FROM sales
ORDER BY id;| id | region | amount | region_total | pct_of_region |
|---|---|---|---|---|
| 1 | East | 100 | 500 | 20.0 |
| 2 | East | 250 | 500 | 50.0 |
| 3 | East | 150 | 500 | 30.0 |
| 4 | West | 400 | 600 | 66.7 |
| 5 | West | 200 | 600 | 33.3 |
| 6 | NULL | 50 | 50 | 100.0 |
The database splits the six rows into three partitions, evaluates SUM(amount) once per partition, and attaches the result to each member row.
Several columns and an empty partition
You can list several columns. Rows form a partition only when they match on all of them:
SELECT
id,
region,
salesperson,
amount,
SUM(amount) OVER () AS grand_total,
SUM(amount) OVER (PARTITION BY region) AS region_total,
SUM(amount) OVER (PARTITION BY region, salesperson) AS person_total
FROM sales
ORDER BY id;| id | region | salesperson | amount | grand_total | region_total | person_total |
|---|---|---|---|---|---|---|
| 1 | East | Ana | 100 | 1150 | 500 | 250 |
| 2 | East | Ben | 250 | 1150 | 500 | 250 |
| 3 | East | Ana | 150 | 1150 | 500 | 250 |
| 4 | West | Cy | 400 | 1150 | 600 | 400 |
| 5 | West | Dee | 200 | 1150 | 600 | 200 |
| 6 | NULL | Eli | 50 | 1150 | 50 | 50 |
This query shows three things:
- Each function has its own partitioning. One
SELECTcan mix as many window definitions as it needs. They do not interfere with each other. OVER ()with noPARTITION BYmakes one partition. All rows of the result belong to it. This is the way to get a grand total beside every row.- More columns mean smaller partitions. Ana's two East sales share a partition with each other, but not with Ben's sale, even though all three are in East.
Partitioning by an expression
The partition key can be any expression, not just a column. This query groups sales into two size classes:
SELECT
id,
amount,
CASE WHEN amount >= 250 THEN 'large' ELSE 'small' END AS size_class,
COUNT(*) OVER (
PARTITION BY CASE WHEN amount >= 250 THEN 'large' ELSE 'small' END
) AS sales_in_class
FROM sales
ORDER BY id;Rows 2 and 4 (amounts 250 and 400) are large with a count of 2. The other four rows are small with a count of 4.
The expression must be written out again inside OVER. In most databases you cannot refer to the size_class alias there, because SELECT aliases are not visible to window definitions. If the expression is long, compute it in a subquery or CTE first, then partition by the resulting column.
NULL values form their own partition
Row 6 has a NULL region. PARTITION BY treats all NULL values in a key as equal, the same way GROUP BY does. Every row with a NULL region lands in one shared partition. If Eli had a second sale with a NULL region, both sales would share one total.
If you want to exclude rows with no region from the calculation, filter them out first. You can also substitute a label with COALESCE(region, 'Unassigned'), in a CTE or inside PARTITION BY.
Partitions restart ranking and ordering
Functions that depend on row order, such as ranking functions, start over at each partition boundary. The next chapters cover ordering and ranking in detail. This short preview shows the effect:
SELECT
region,
salesperson,
amount,
ROW_NUMBER() OVER (PARTITION BY region ORDER BY amount DESC) AS rn
FROM sales
ORDER BY region, rn;| region | salesperson | amount | rn |
|---|---|---|---|
| East | Ben | 250 | 1 |
| East | Ana | 150 | 2 |
| East | Ana | 100 | 3 |
| West | Cy | 400 | 1 |
| West | Dee | 200 | 2 |
| NULL | Eli | 50 | 1 |
The numbering runs from 1 inside each partition. Without PARTITION BY, the same query would number all six rows from 1 to 6.
The final ORDER BY is not on the page by accident. Some databases sort rows by the partition key as a side effect of how they compute the window, but nothing in SQL guarantees that. Always add an outer ORDER BY when the display order matters. Where NULL sorts relative to other values also differs between databases.
How WHERE affects partitions
A window function runs after WHERE, GROUP BY and HAVING have been applied. A row removed by WHERE is not in any partition. It does not contribute to any total:
SELECT
id,
region,
amount,
SUM(amount) OVER (PARTITION BY region) AS region_total
FROM sales
WHERE amount >= 150
ORDER BY id;| id | region | amount | region_total |
|---|---|---|---|
| 2 | East | 250 | 400 |
| 3 | East | 150 | 400 |
| 4 | West | 400 | 600 |
| 5 | West | 200 | 600 |
East's total is now 400, not 500, because row 1 (amount 100) was removed before the window ran. If you want totals over all rows but a filtered display, compute the window in a CTE or subquery, then filter in the outer query. The chapter on evaluation order covers this in full.
Common mistakes
- Partitioning by a unique column.
PARTITION BY idmakes every row its own partition, soSUM(amount)just returnsamount. If a per-group result looks identical to the raw column, check that the partition key actually repeats. - Partitioning too coarsely. If
PARTITION BY regionshould have beenPARTITION BY region, salesperson, you get plausible-looking but wrong totals. Compare a few partition totals by hand against aGROUP BYquery on the same keys. - Expecting fewer rows.
PARTITION BYnever removes or merges rows. If you need one row per group, useGROUP BY, or pick one row per partition with a ranking filter. - Using a window function in
WHERE.WHERE SUM(amount) OVER (...) > 500is an error in every database listed above. Move the calculation into a CTE and filter outside it. - Relying on partition order. Partitions have no inherent order. Order rows with the window's
ORDER BYfor calculations and with the outerORDER BYfor display.
Summary
PARTITION BY splits the rows seen by a window function into independent groups. Each row stays in the output and sees only its own group. Several columns or an expression define finer groups. OVER () with no clause makes a single group. NULL keys share one partition, and WHERE removes rows before partitions form.
Exercise
Write a query that returns every row of sales with a column salesinregion, the number of sales in that row's region. What value does Eli's row (id 6, NULL region) get, and why?
Show answer
Use COUNT(*) OVER (PARTITION BY region) AS salesinregion. Eli's row gets 1. PARTITION BY puts all NULL region values into one partition, and Eli's is the only sale with a NULL region.
Exercise
What does SUM(amount) OVER (PARTITION BY id) return for each row of sales, and why is it rarely useful?
Show answer
It returns the row's own amount. The id column is unique, so every partition holds exactly one row, and the sum over one row is that row's value. A useful partition key must repeat across rows.