0
0
SQLquery~20 mins

OVER clause with ORDER BY in SQL - 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
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;
A[{"employee_id":1,"sale_date":"2023-01-01","amount":100,"running_total":100},{"employee_id":1,"sale_date":"2023-01-05","amount":150,"running_total":250},{"employee_id":2,"sale_date":"2023-01-02","amount":200,"running_total":200},{"employee_id":2,"sale_date":"2023-01-06","amount":100,"running_total":300}]
B[{"employee_id":1,"sale_date":"2023-01-01","amount":100,"running_total":100},{"employee_id":1,"sale_date":"2023-01-05","amount":150,"running_total":150},{"employee_id":2,"sale_date":"2023-01-02","amount":200,"running_total":200},{"employee_id":2,"sale_date":"2023-01-06","amount":100,"running_total":100}]
C[{"employee_id":1,"sale_date":"2023-01-01","amount":100,"running_total":100},{"employee_id":1,"sale_date":"2023-01-05","amount":150,"running_total":250},{"employee_id":2,"sale_date":"2023-01-02","amount":200,"running_total":100},{"employee_id":2,"sale_date":"2023-01-06","amount":100,"running_total":300}]
D[{"employee_id":1,"sale_date":"2023-01-01","amount":100,"running_total":250},{"employee_id":1,"sale_date":"2023-01-05","amount":150,"running_total":250},{"employee_id":2,"sale_date":"2023-01-02","amount":200,"running_total":300},{"employee_id":2,"sale_date":"2023-01-06","amount":100,"running_total":300}]
Attempts:
2 left
💡 Hint
Think about how the SUM() function accumulates values ordered by sale_date for each employee separately.
🧠 Conceptual
intermediate
1: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:
SELECT id, value, ROW_NUMBER() OVER (ORDER BY value DESC) AS rank FROM Items;
AIt assigns a unique rank to each row based on descending value across the entire table.
BIt resets the rank for each distinct value in the value column.
CIt produces the same rank for all rows because there is no partition.
DIt causes a syntax error because PARTITION BY is required with ORDER BY in OVER().
Attempts:
2 left
💡 Hint
Think about how window functions rank rows when no partition is specified.
📝 Syntax
advanced
1: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:
SELECT name, salary, RANK() OVER (ORDER BY salary DESC) FROM Employees;
ASELECT name, salary, RANK() OVER (PARTITION salary ORDER BY DESC) FROM Employees;
BSELECT name, salary, RANK() OVER ORDER BY salary DESC FROM Employees;
CSELECT name, salary, RANK() OVER (ORDER BY salary DESC) FROM Employees;
DSELECT name, salary, RANK() OVER (ORDER salary DESC) FROM Employees;
Attempts:
2 left
💡 Hint
Check the placement and syntax of the OVER clause and ORDER BY inside it.
optimization
advanced
2: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?
AUse a correlated subquery to sum amounts up to each transaction_date.
BUse SUM(amount) OVER (ORDER BY transaction_date) with an index on transaction_date.
CUse GROUP BY transaction_date and then join back to the original table.
DCalculate running total in application code after fetching all rows unordered.
Attempts:
2 left
💡 Hint
Window functions with proper indexing can be very efficient.
🔧 Debug
expert
2:30remaining
Why does this query produce unexpected results?
Consider this query:
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?
ABecause ORDER BY in OVER() must match ORDER BY in the main query to reset the total.
BBecause sale_date values are not unique, causing incorrect running totals.
CBecause SUM() cannot be used with ORDER BY in OVER() without GROUP BY.
DBecause PARTITION BY employee_id is missing, so the running total is calculated over all rows ordered by sale_date.
Attempts:
2 left
💡 Hint
Think about how PARTITION BY affects window function grouping.