Challenge - 5 Problems
Window Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Calculate running total of sales per store
Given a table
sales with columns store_id, sale_date, and amount, what is the running total of sales per store ordered by sale_date?PostgreSQL
SELECT store_id, sale_date, amount, SUM(amount) OVER (PARTITION BY store_id ORDER BY sale_date) AS running_total FROM sales ORDER BY store_id, sale_date;
Attempts:
2 left
💡 Hint
Think about how the SUM window function accumulates values within each store partition ordered by date.
✗ Incorrect
The SUM() window function with PARTITION BY store_id and ORDER BY sale_date calculates a running total of sales per store in date order. Option A correctly shows cumulative sums per store.
❓ query_result
intermediate2:00remaining
Find the rank of employees by salary within each department
Given a table
employees with columns department_id, employee_name, and salary, which query returns the rank of each employee's salary within their department, with the highest salary ranked 1?PostgreSQL
SELECT department_id, employee_name, salary, RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS salary_rank FROM employees ORDER BY department_id, salary_rank;
Attempts:
2 left
💡 Hint
Use RANK() with PARTITION BY department and ORDER BY salary descending.
✗ Incorrect
RANK() assigns ranks starting at 1 for the highest salary per department. Option B correctly ranks employees by salary within each department.
📝 Syntax
advanced2:00remaining
Identify the syntax error in this window function query
Which option contains a syntax error when trying to calculate the moving average of sales over the last 3 days per store?
PostgreSQL
SELECT store_id, sale_date, amount, AVG(amount) OVER (PARTITION BY store_id ORDER BY sale_date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS moving_avg FROM sales;
Attempts:
2 left
💡 Hint
Check the syntax of the ROWS clause in the window frame specification.
✗ Incorrect
Option D is missing the keyword BETWEEN before the frame boundaries, causing a syntax error. The correct syntax requires 'ROWS BETWEEN x PRECEDING AND y CURRENT ROW'.
❓ optimization
advanced2:00remaining
Optimize query to find first and last sale date per customer
Which query is the most efficient to get the first and last sale date per customer from a
sales table with many rows?Attempts:
2 left
💡 Hint
Consider the cost of window functions versus aggregate functions on large datasets.
✗ Incorrect
Option C uses simple aggregation with GROUP BY, which is more efficient than window functions or correlated subqueries for large tables. Options A, C, and D are less efficient due to window function overhead or multiple subqueries.
🧠 Conceptual
expert2:00remaining
Understanding difference between ROWS and RANGE in window frames
What is the key difference between using
ROWS BETWEEN 1 PRECEDING AND CURRENT ROW and RANGE BETWEEN 1 PRECEDING AND CURRENT ROW in a window function ordered by a numeric column?Attempts:
2 left
💡 Hint
Think about how physical row count differs from value-based range in window frames.
✗ Incorrect
ROWS defines the frame by counting a fixed number of physical rows relative to the current row. RANGE defines the frame by including all rows with values within a specified range of the current row's ordering value, which can include multiple rows with the same value.