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 with window functions
Given a sales table with columns
sale_date and amount, what is the output of this query?SELECT sale_date, amount, SUM(amount) OVER (ORDER BY sale_date) AS running_total FROM sales ORDER BY sale_date;
SQL
CREATE TABLE sales (sale_date DATE, amount INT); INSERT INTO sales VALUES ('2024-01-01', 100), ('2024-01-02', 200), ('2024-01-03', 150);
Attempts:
2 left
💡 Hint
Think about how the SUM() window function accumulates values ordered by date.
✗ Incorrect
The SUM() OVER (ORDER BY sale_date) calculates a running total by adding amounts in order of sale_date. So the totals accumulate as 100, then 100+200=300, then 300+150=450.
❓ query_result
intermediate2:00remaining
Find the rank of employees by salary within departments
Consider an
employees table with columns department and salary. What is the output of this query?SELECT department, salary, RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS salary_rank FROM employees ORDER BY department, salary_rank;
SQL
CREATE TABLE employees (department TEXT, salary INT); INSERT INTO employees VALUES ('Sales', 5000), ('Sales', 7000), ('Sales', 7000), ('HR', 4000), ('HR', 4500);
Attempts:
2 left
💡 Hint
RANK() assigns the same rank to ties and skips ranks after ties.
✗ Incorrect
Within each department, salaries are ranked descending. Ties get the same rank, and the next rank skips accordingly. So two 7000 salaries in Sales get rank 1, next salary 5000 gets rank 3.
📝 Syntax
advanced2:00remaining
Identify the syntax error in window function usage
Which option contains a syntax error in the use of window functions?
Attempts:
2 left
💡 Hint
Check the framing clause syntax carefully.
✗ Incorrect
Option D is missing the keyword BETWEEN in the frame clause. Correct syntax requires BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW.
❓ optimization
advanced2:00remaining
Optimize query using window functions instead of subqueries
Which query is more efficient for calculating each employee's salary difference from the department average?
SQL
CREATE TABLE employees (id INT, department TEXT, salary INT); INSERT INTO employees VALUES (1, 'HR', 4000), (2, 'HR', 4500), (3, 'Sales', 7000), (4, 'Sales', 5000);
Attempts:
2 left
💡 Hint
Window functions can avoid repeated subqueries for each row.
✗ Incorrect
Option B uses AVG() as a window function partitioned by department, calculating the average once per partition efficiently. Option B runs a subquery per row, which is less efficient.
🧠 Conceptual
expert2:00remaining
Why use advanced window functions over GROUP BY?
Which statement best explains why advanced window functions matter compared to simple GROUP BY aggregation?
Attempts:
2 left
💡 Hint
Think about how window functions keep rows while adding calculations.
✗ Incorrect
Window functions compute aggregates or rankings over partitions without reducing the number of rows, unlike GROUP BY which groups and collapses rows. This allows more detailed and flexible analysis.