Stop 08 · Window FunctionsTopics · 2/9

PARTITION BY

Part of the SQL Roadmap

In short

A grand total is easy. The real questions sound like "show every order next to that customer's total". PARTITION BY restarts the calculation for each group and still keeps every single row on screen.

PARTITION BY splits the window into groups, so the calculation restarts for each group, like a separate total per customer or per category.

The jaadu sits behind a kirana shop counter, flipping a thick ledger to a fresh page for each customer waiting in line with shopping bags.

Think of it like this

One page per customer in the khata

A kirana shopkeeper keeps one big khata, but every customer gets their own page. The total at the bottom of Riya's page only adds Riya's purchases. PARTITION BY customer_id gives each customer their own page, and the window function totals each page separately.

Watch it happen

SUM(amount) OVER (PARTITION BY customer)

Riya ₹500
0
Riya ₹300
1
Riya ₹200
2
Kabir ₹400
3
Kabir ₹100
4

1/4Five orders from two customers. With an empty OVER (), every row would show the grand total, ₹1,500.

The big ideas

What you need to know

Restart per group

PARTITION BY splits rows into groups. The window function runs again from scratch inside each group.

Rows stay put

GROUP BY squashes a customer into one row. PARTITION BY keeps every order and just adds a new column.

Several columns work

PARTITION BY city, category gives one window per city and category pair, like Pune Electronics or Delhi Books.

Add ORDER BY later

Put ORDER BY after PARTITION BY inside OVER and you get rankings or running totals per group.

See it in code

Walk through the code

sql
1SELECT2  order_id,3  customer_id,4  amount,5  SUM(amount) OVER (PARTITION BY customer_id) AS customer_total,6  ROUND(7    100.0 * amount / SUM(amount) OVER (PARTITION BY customer_id),8    19  ) AS pct_of_customer10FROM orders11ORDER BY customer_id, order_id;

1/4Plain columns. Every order row stays in the result.

Each order with its customer's total spend and its share of that spend. Works the same in PostgreSQL, MySQL 8+ and SQL Server.

Save yourself the pain

Common mistakes

Oops

Using GROUP BY customer_id and then wondering where the order details went.

Do this instead

If you need each row plus a group number, use SUM(...) OVER (PARTITION BY customer_id) instead.

Oops

Partitioning by customer_name.

Do this instead

Two customers can both be called Amit Sharma. Partition by customer_id so different people never share a window.

Your mission

Each order and its customer

~20 min

Quick check

Riya has 3 orders and Kabir has 2. How many rows does SELECT *, SUM(amount) OVER (PARTITION BY customer_id) FROM orders return?

Go deeper

  1. 1Show each order next to that customer's total spend.
  2. 2Calculate each product's share of its category's revenue.
  3. 3Add ORDER BY inside OVER and notice how the result changes.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge