Advanced SQL: Window Functions|
Understanding PARTITION BY

PARTITION BY Basics

SQL

Understanding PARTITION BY

PARTITION BY

PARTITION BY divides rows into groups for calculation:

SELECT 
    name, 
    category, 
    price,
    AVG(price) OVER(PARTITION BY category) as category_avg
FROM products;

This calculates the average within each category, while keeping all rows.

Your Task

For each product, show:

  • name, category, price
  • The average price within its category (as category_avg)

    Order by category, then price descending.

Query Editor
Loading SQL engine...
Loading...
Loading SQL engine...

Run your query to see results