0
0
PostgreSQLquery~20 mins

PARTITION BY for grouping windows in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Window Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of ROW_NUMBER() with PARTITION BY
Given the table sales with columns region, salesperson, and amount, what is the output of this query?
SELECT region, salesperson, amount,
ROW_NUMBER() OVER (PARTITION BY region ORDER BY amount DESC) AS rank
FROM sales;
PostgreSQL
CREATE TABLE sales (region TEXT, salesperson TEXT, amount INT);
INSERT INTO sales VALUES
('North', 'Alice', 500),
('North', 'Bob', 300),
('South', 'Carol', 700),
('South', 'Dave', 400);

SELECT region, salesperson, amount,
       ROW_NUMBER() OVER (PARTITION BY region ORDER BY amount DESC) AS rank
FROM sales;
A[{"region": "North", "salesperson": "Alice", "amount": 500, "rank": 1}, {"region": "North", "salesperson": "Bob", "amount": 300, "rank": 2}, {"region": "South", "salesperson": "Carol", "amount": 700, "rank": 1}, {"region": "South", "salesperson": "Dave", "amount": 400, "rank": 2}]
B[{"region": "North", "salesperson": "Alice", "amount": 500, "rank": 2}, {"region": "North", "salesperson": "Bob", "amount": 300, "rank": 1}, {"region": "South", "salesperson": "Carol", "amount": 700, "rank": 2}, {"region": "South", "salesperson": "Dave", "amount": 400, "rank": 1}]
C[{"region": "North", "salesperson": "Alice", "amount": 500, "rank": 1}, {"region": "North", "salesperson": "Bob", "amount": 300, "rank": 1}, {"region": "South", "salesperson": "Carol", "amount": 700, "rank": 1}, {"region": "South", "salesperson": "Dave", "amount": 400, "rank": 1}]
D[{"region": "North", "salesperson": "Alice", "amount": 500, "rank": 2}, {"region": "North", "salesperson": "Bob", "amount": 300, "rank": 2}, {"region": "South", "salesperson": "Carol", "amount": 700, "rank": 2}, {"region": "South", "salesperson": "Dave", "amount": 400, "rank": 2}]
Attempts:
2 left
💡 Hint
Remember that ROW_NUMBER() restarts numbering for each partition defined by PARTITION BY.
query_result
intermediate
2:00remaining
SUM() with PARTITION BY in window functions
What is the output of this query on the orders table with columns customer_id and order_total?
SELECT customer_id, order_total,
SUM(order_total) OVER (PARTITION BY customer_id) AS total_per_customer
FROM orders
ORDER BY customer_id, order_total;
PostgreSQL
CREATE TABLE orders (customer_id INT, order_total INT);
INSERT INTO orders VALUES
(1, 100),
(1, 200),
(2, 150),
(2, 50);

SELECT customer_id, order_total,
       SUM(order_total) OVER (PARTITION BY customer_id) AS total_per_customer
FROM orders
ORDER BY customer_id, order_total;
A[{"customer_id": 1, "order_total": 100, "total_per_customer": 100}, {"customer_id": 1, "order_total": 200, "total_per_customer": 200}, {"customer_id": 2, "order_total": 50, "total_per_customer": 50}, {"customer_id": 2, "order_total": 150, "total_per_customer": 150}]
B[{"customer_id": 1, "order_total": 100, "total_per_customer": 300}, {"customer_id": 1, "order_total": 200, "total_per_customer": 300}, {"customer_id": 2, "order_total": 50, "total_per_customer": 200}, {"customer_id": 2, "order_total": 150, "total_per_customer": 200}]
C[{"customer_id": 1, "order_total": 100, "total_per_customer": 100}, {"customer_id": 1, "order_total": 200, "total_per_customer": 300}, {"customer_id": 2, "order_total": 50, "total_per_customer": 50}, {"customer_id": 2, "order_total": 150, "total_per_customer": 200}]
D[{"customer_id": 1, "order_total": 100, "total_per_customer": 300}, {"customer_id": 1, "order_total": 200, "total_per_customer": 200}, {"customer_id": 2, "order_total": 50, "total_per_customer": 200}, {"customer_id": 2, "order_total": 150, "total_per_customer": 150}]
Attempts:
2 left
💡 Hint
SUM() with PARTITION BY calculates the total for each group and repeats it for every row in that group.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in window function usage
Which option contains a syntax error in using PARTITION BY in a window function?
SELECT employee_id, department,
RANK() OVER (PARTITION department ORDER BY salary DESC) AS dept_rank
FROM employees;
ARANK() OVER (PARTITION BY department ORDER BY salary DESC)
BRANK() OVER (PARTITION BY department ORDER BY salary DESC NULLS FIRST)
CRANK() OVER (PARTITION BY department ORDER BY salary DESC NULLS LAST)
DRANK() OVER (PARTITION department ORDER BY salary DESC)
Attempts:
2 left
💡 Hint
Check the keyword used for grouping in window functions.
optimization
advanced
2:00remaining
Optimizing window function with PARTITION BY
You want to calculate the average sales per region and show it alongside each sale. Which query is more efficient?
A) SELECT region, sale_amount,
AVG(sale_amount) OVER (PARTITION BY region) AS avg_region_sale
FROM sales;

B) SELECT s.region, s.sale_amount, r.avg_sale
FROM sales s
JOIN (SELECT region, AVG(sale_amount) AS avg_sale FROM sales GROUP BY region) r ON s.region = r.region;
AQuery A is more efficient because window functions avoid extra joins and scan the table once.
BQuery B is more efficient because JOIN with aggregation is faster than window functions.
CBoth queries have the same efficiency because they produce the same result.
DQuery B is more efficient because it uses GROUP BY which is always faster than window functions.
Attempts:
2 left
💡 Hint
Window functions compute aggregates without extra joins, often improving performance.
🧠 Conceptual
expert
2:00remaining
Understanding difference between PARTITION BY and GROUP BY
Which statement correctly explains the difference between PARTITION BY in window functions and GROUP BY in SQL?
ABoth PARTITION BY and GROUP BY produce the same output but use different syntax.
BPARTITION BY aggregates rows into one row per group; GROUP BY keeps all rows but divides them into groups.
CPARTITION BY divides rows into groups but keeps all rows in the output; GROUP BY aggregates rows into one row per group.
DPARTITION BY is used only for filtering rows; GROUP BY is used only for sorting rows.
Attempts:
2 left
💡 Hint
Think about whether the number of rows changes after applying each clause.