Challenge - 5 Problems
Window Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2: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;
Attempts:
2 left
💡 Hint
Remember that ROW_NUMBER() restarts numbering for each partition defined by PARTITION BY.
✗ Incorrect
ROW_NUMBER() assigns a unique rank starting at 1 within each partition. Here, partitioning by region means numbering restarts for 'North' and 'South' separately, ordered by amount descending.
❓ query_result
intermediate2: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;
Attempts:
2 left
💡 Hint
SUM() with PARTITION BY calculates the total for each group and repeats it for every row in that group.
✗ Incorrect
The window SUM() adds all order_totals per customer_id and shows the same total for each row of that customer.
📝 Syntax
advanced2: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;
Attempts:
2 left
💡 Hint
Check the keyword used for grouping in window functions.
✗ Incorrect
The correct keyword is PARTITION BY, not PARTITION alone. Option D misses the BY keyword causing syntax error.
❓ optimization
advanced2: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;
Attempts:
2 left
💡 Hint
Window functions compute aggregates without extra joins, often improving performance.
✗ Incorrect
Window functions compute aggregates over partitions in the same scan, avoiding costly joins and improving efficiency.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Think about whether the number of rows changes after applying each clause.
✗ Incorrect
PARTITION BY groups rows for window functions but returns all original rows with extra info; GROUP BY reduces rows to one per group by aggregation.