0
0
SQLquery~20 mins

Why advanced window functions matter in SQL - Challenge Your Understanding

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 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);
A[{"sale_date": "2024-01-01", "amount": 100, "running_total": 100}, {"sale_date": "2024-01-02", "amount": 200, "running_total": 300}, {"sale_date": "2024-01-03", "amount": 150, "running_total": 450}]
B[{"sale_date": "2024-01-01", "amount": 100, "running_total": 100}, {"sale_date": "2024-01-02", "amount": 200, "running_total": 200}, {"sale_date": "2024-01-03", "amount": 150, "running_total": 150}]
C[{"sale_date": "2024-01-01", "amount": 100, "running_total": 450}, {"sale_date": "2024-01-02", "amount": 200, "running_total": 350}, {"sale_date": "2024-01-03", "amount": 150, "running_total": 150}]
D[{"sale_date": "2024-01-01", "amount": 100, "running_total": null}, {"sale_date": "2024-01-02", "amount": 200, "running_total": null}, {"sale_date": "2024-01-03", "amount": 150, "running_total": null}]
Attempts:
2 left
💡 Hint
Think about how the SUM() window function accumulates values ordered by date.
query_result
intermediate
2: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);
A[{"department": "HR", "salary": 4500, "salary_rank": 2}, {"department": "HR", "salary": 4000, "salary_rank": 1}, {"department": "Sales", "salary": 7000, "salary_rank": 1}, {"department": "Sales", "salary": 7000, "salary_rank": 1}, {"department": "Sales", "salary": 5000, "salary_rank": 2}]
B[{"department": "HR", "salary": 4500, "salary_rank": 1}, {"department": "HR", "salary": 4000, "salary_rank": 1}, {"department": "Sales", "salary": 7000, "salary_rank": 1}, {"department": "Sales", "salary": 7000, "salary_rank": 2}, {"department": "Sales", "salary": 5000, "salary_rank": 3}]
C[{"department": "HR", "salary": 4500, "salary_rank": 1}, {"department": "HR", "salary": 4000, "salary_rank": 2}, {"department": "Sales", "salary": 7000, "salary_rank": 1}, {"department": "Sales", "salary": 7000, "salary_rank": 1}, {"department": "Sales", "salary": 5000, "salary_rank": 3}]
D[{"department": "HR", "salary": 4500, "salary_rank": 1}, {"department": "HR", "salary": 4000, "salary_rank": 2}, {"department": "Sales", "salary": 7000, "salary_rank": 2}, {"department": "Sales", "salary": 7000, "salary_rank": 2}, {"department": "Sales", "salary": 5000, "salary_rank": 3}]
Attempts:
2 left
💡 Hint
RANK() assigns the same rank to ties and skips ranks after ties.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in window function usage
Which option contains a syntax error in the use of window functions?
ASELECT name, AVG(score) OVER (PARTITION BY team ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM players;
BSELECT name, MAX(score) OVER (PARTITION BY team ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM players;
CSELECT name, SUM(score) OVER (ORDER BY date RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM players;
DSELECT name, COUNT(*) OVER (PARTITION BY team ORDER BY date ROWS UNBOUNDED PRECEDING) FROM players;
Attempts:
2 left
💡 Hint
Check the framing clause syntax carefully.
optimization
advanced
2: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);
ASELECT id, department, salary, salary - (SELECT AVG(salary) FROM employees e2 WHERE e2.department = e1.department) AS diff FROM employees e1;
BSELECT id, department, salary, salary - AVG(salary) OVER (PARTITION BY department) AS diff FROM employees;
CSELECT id, department, salary, salary - SUM(salary) OVER (PARTITION BY department) / COUNT(*) OVER (PARTITION BY department) AS diff FROM employees;
DSELECT id, department, salary, salary - MAX(salary) OVER (PARTITION BY department) AS diff FROM employees;
Attempts:
2 left
💡 Hint
Window functions can avoid repeated subqueries for each row.
🧠 Conceptual
expert
2:00remaining
Why use advanced window functions over GROUP BY?
Which statement best explains why advanced window functions matter compared to simple GROUP BY aggregation?
AWindow functions allow calculations across rows related to the current row without collapsing rows, enabling detailed analysis within groups.
BWindow functions always run faster than GROUP BY because they use indexes automatically.
CGROUP BY can only be used with numeric columns, while window functions work with all data types.
DWindow functions replace the need for JOINs in all cases, simplifying queries.
Attempts:
2 left
💡 Hint
Think about how window functions keep rows while adding calculations.