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 employee
Given the Sales table with columns
employee_id, sale_date, and amount, what is the output of this query?SELECT employee_id, sale_date, amount, SUM(amount) OVER (PARTITION BY employee_id ORDER BY sale_date) AS running_total FROM Sales ORDER BY employee_id, sale_date;
SQL
SELECT employee_id, sale_date, amount, SUM(amount) OVER (PARTITION BY employee_id ORDER BY sale_date) AS running_total FROM Sales ORDER BY employee_id, sale_date;
Attempts:
2 left
💡 Hint
Think about how the SUM() function accumulates values ordered by sale_date for each employee separately.
✗ Incorrect
The SUM() OVER clause with PARTITION BY employee_id and ORDER BY sale_date calculates a running total of sales for each employee ordered by the sale date. The running total adds the current amount to the sum of all previous amounts for that employee.
🧠 Conceptual
intermediate1:30remaining
Effect of ORDER BY in OVER clause without PARTITION BY
What does the ORDER BY clause inside the OVER() do when used without PARTITION BY?
Consider this query:
Consider this query:
SELECT id, value, ROW_NUMBER() OVER (ORDER BY value DESC) AS rank FROM Items;
Attempts:
2 left
💡 Hint
Think about how window functions rank rows when no partition is specified.
✗ Incorrect
Without PARTITION BY, the window function treats the entire table as one group. ORDER BY inside OVER() defines the order in which the function processes rows. ROW_NUMBER() assigns a unique rank based on that order.
📝 Syntax
advanced1:30remaining
Identify the syntax error in this window function
Which option contains the correct syntax for using the OVER clause with ORDER BY?
Given the query:
Given the query:
SELECT name, salary, RANK() OVER (ORDER BY salary DESC) FROM Employees;
Attempts:
2 left
💡 Hint
Check the placement and syntax of the OVER clause and ORDER BY inside it.
✗ Incorrect
The correct syntax requires parentheses after OVER and ORDER BY inside those parentheses. Option C follows this syntax exactly.
❓ optimization
advanced2:00remaining
Optimizing window function with ORDER BY on large dataset
You have a large
Transactions table and want to calculate a running total of amount ordered by transaction_date. Which approach is best for performance?Attempts:
2 left
💡 Hint
Window functions with proper indexing can be very efficient.
✗ Incorrect
Using SUM() OVER (ORDER BY ...) with an index on the ordering column allows the database to efficiently compute running totals without expensive joins or subqueries.
🔧 Debug
expert2:30remaining
Why does this query produce unexpected results?
Consider this query:
Why might the running_total not reset for each employee?
SELECT employee_id, sale_date, amount, SUM(amount) OVER (ORDER BY sale_date) AS running_total FROM Sales ORDER BY employee_id, sale_date;
Why might the running_total not reset for each employee?
Attempts:
2 left
💡 Hint
Think about how PARTITION BY affects window function grouping.
✗ Incorrect
Without PARTITION BY, the window function treats all rows as one group, so the running total accumulates over all employees combined, not resetting per employee.