Sub topic 3
Splitting Rows into Partitions with `PARTITION BY`
Prerequisites
Assume you know SELECT, WHERE, ORDER BY, GROUP BY, aggregate functions such as SUM and COUNT, and common table expressions. You also need a database that supports window functions, such as PostgreSQL, MySQL 8+, SQL Server, SQLite 3.25+, or Oracle.
What a partition is
A window function calculates a value from related rows while keeping the original rows in the result. PARTITION BY defines which rows are related.
Each distinct value in the partition expression creates a separate group for the window calculation. Unlike GROUP BY, partitioning does not combine the rows into one result row.
Consider this sample table:
CREATE TABLE sales (
id INTEGER PRIMARY KEY,
region VARCHAR(20),
salesperson VARCHAR(30),
amount DECIMAL(10, 2)
);
INSERT INTO sales (id, region, salesperson, amount) VALUES
(1, 'East', 'Ava', 1200.00),
(2, 'East', 'Ben', 800.00),
(3, 'West', 'Cara', 1500.00),
(4, 'West', 'Diego', 500.00);This query calculates a separate total for each region:
SELECT
id,
region,
salesperson,
amount,
SUM(amount) OVER (PARTITION BY region) AS region_total
FROM sales;The result contains four rows. The two East rows each show 2000.00, and the two West rows each show 2000.00. The calculation is repeated on every row in its partition.
| id | region | salesperson | amount | region_total |
|---|---|---|---|---|
| 1 | East | Ava | 1200.00 | 2000.00 |
| 2 | East | Ben | 800.00 | 2000.00 |
| 3 | West | Cara | 1500.00 | 2000.00 |
| 4 | West | Diego | 500.00 | 2000.00 |
A GROUP BY query would return only one row per region. A window query can show both the detail and the regional summary together.
How partitions are formed
The database evaluates the expression after PARTITION BY for each row. Rows with equal values go into the same partition.
You can partition by more than one column. This creates a partition for each combination:
SELECT
region,
salesperson,
amount,
SUM(amount) OVER (
PARTITION BY region, salesperson
) AS salesperson_region_total
FROM sales;If salesperson is unique in each region, the total equals that person's row amount. In a larger table with several sales per salesperson, it would show that salesperson's regional total on every matching row.
NULL values are treated as equal for partitioning. Therefore, all rows whose partition expression is NULL belong to the same partition.
Combining partitioning with ordering
PARTITION BY chooses the groups. An ORDER BY inside the same OVER clause establishes row order within each group. This matters for ranking, running totals, and functions such as LAG.
SELECT
region,
salesperson,
amount,
SUM(amount) OVER (
PARTITION BY region
) AS region_total,
amount * 100.0 / SUM(amount) OVER (
PARTITION BY region
) AS percent_of_region
FROM sales
ORDER BY region, amount DESC;The outer ORDER BY sorts the displayed result. It is separate from the ORDER BY inside OVER, which controls how a window function processes rows.
Exercise
What does SUM(amount) OVER (PARTITION BY region) return for each row?
Show answer
It returns the sum of amount for that row's region, while keeping the row itself in the result.