Learn SQL Lesson
PARTITION BY Creates Mini Windows
`PARTITION BY` splits the result set into smaller windows before the function runs.
If you want each sale to carry its region total, you do not want one giant company-wide window anymore. You want one window per region:
~~~sql SELECT id, region, amount, SUM(amount) OVER (PARTITION BY region) AS region_total FROM sales_2 ~~~
Now every East row gets the East total, every West row gets the West total, and so on.
This is one of the most useful patterns in SQL because it lets you compare a row to its group without losing row-level detail.
Practice challenge
Show each sale with its regional total. Return id, region, amount, and region_total, ordered by id.