0
0
SQLquery~20 mins

OVER clause with PARTITION 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 per department
Given the sales table with columns department, sale_date, and amount, what is the output of this query?
SELECT department, sale_date, amount,
SUM(amount) OVER (PARTITION BY department ORDER BY sale_date) AS running_total
FROM sales
ORDER BY department, sale_date;
SQL
CREATE TABLE sales (department VARCHAR(20), sale_date DATE, amount INT);
INSERT INTO sales VALUES
('Electronics', '2024-01-01', 100),
('Electronics', '2024-01-02', 150),
('Clothing', '2024-01-01', 200),
('Clothing', '2024-01-03', 100);
A[{"department": "Clothing", "sale_date": "2024-01-01", "amount": 200, "running_total": 200}, {"department": "Clothing", "sale_date": "2024-01-03", "amount": 100, "running_total": 300}, {"department": "Electronics", "sale_date": "2024-01-01", "amount": 100, "running_total": 100}, {"department": "Electronics", "sale_date": "2024-01-02", "amount": 150, "running_total": 250}]
B[{"department": "Clothing", "sale_date": "2024-01-01", "amount": 200, "running_total": 200}, {"department": "Clothing", "sale_date": "2024-01-03", "amount": 100, "running_total": 100}, {"department": "Electronics", "sale_date": "2024-01-01", "amount": 100, "running_total": 100}, {"department": "Electronics", "sale_date": "2024-01-02", "amount": 150, "running_total": 150}]
C[{"department": "Clothing", "sale_date": "2024-01-01", "amount": 200, "running_total": 200}, {"department": "Clothing", "sale_date": "2024-01-03", "amount": 100, "running_total": 300}, {"department": "Electronics", "sale_date": "2024-01-01", "amount": 100, "running_total": 250}, {"department": "Electronics", "sale_date": "2024-01-02", "amount": 150, "running_total": 250}]
D[{"department": "Clothing", "sale_date": "2024-01-01", "amount": 200, "running_total": 100}, {"department": "Clothing", "sale_date": "2024-01-03", "amount": 100, "running_total": 300}, {"department": "Electronics", "sale_date": "2024-01-01", "amount": 100, "running_total": 100}, {"department": "Electronics", "sale_date": "2024-01-02", "amount": 150, "running_total": 250}]
Attempts:
2 left
💡 Hint
Think about how the running total sums amounts within each department ordered by date.
📝 Syntax
intermediate
1:30remaining
Identify the syntax error in window function usage
Which option contains a syntax error in using the OVER clause with PARTITION BY?
ASELECT name, salary, DENSE_RANK() OVER (PARTITION BY department ORDER BY salary DESC) FROM employees;
BSELECT name, salary, RANK() OVER PARTITION BY department ORDER BY salary DESC FROM employees;
CSELECT name, salary, ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) FROM employees;
DSELECT name, salary, RANK() OVER (PARTITION BY department ORDER BY salary DESC) FROM employees;
Attempts:
2 left
💡 Hint
Check the parentheses around the OVER clause.
optimization
advanced
2:30remaining
Optimize query with multiple window functions
You want to calculate both the rank and the cumulative sum of sales per region ordered by date. Which query is more efficient?
A
SELECT region, sale_date, amount,
       RANK() OVER (PARTITION BY region ORDER BY sale_date) AS rank
FROM sales;

SELECT region, sale_date, amount,
       SUM(amount) OVER (PARTITION BY region ORDER BY sale_date) AS running_sum
FROM sales;
B
SELECT region, sale_date, amount,
       RANK() OVER (PARTITION BY region) AS rank,
       SUM(amount) OVER (PARTITION BY region) AS running_sum
FROM sales;
C
SELECT region, sale_date, amount,
       RANK() OVER (ORDER BY sale_date) AS rank,
       SUM(amount) OVER (ORDER BY sale_date) AS running_sum
FROM sales;
D
SELECT region, sale_date, amount,
       RANK() OVER (PARTITION BY region ORDER BY sale_date) AS rank,
       SUM(amount) OVER (PARTITION BY region ORDER BY sale_date) AS running_sum
FROM sales;
Attempts:
2 left
💡 Hint
Consider how many times the table is scanned and how partitioning affects results.
🔧 Debug
advanced
2:00remaining
Find the cause of incorrect ranking results
A query uses RANK() OVER (PARTITION BY department) without ORDER BY. Why might the ranking be incorrect or unexpected?
ABecause ORDER BY is missing, the ranking is arbitrary and not based on any column, causing unexpected results.
BBecause PARTITION BY is missing, the ranking is calculated over the entire table, not per department.
CBecause RANK() cannot be used with PARTITION BY, it causes a runtime error.
DBecause the query lacks GROUP BY, the ranking cannot be computed correctly.
Attempts:
2 left
💡 Hint
Think about what ORDER BY does inside the OVER clause.
🧠 Conceptual
expert
3:00remaining
Understanding frame specification with PARTITION BY
Consider this query:
SELECT department, sale_date, amount,
SUM(amount) OVER (PARTITION BY department ORDER BY sale_date ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS moving_sum
FROM sales
ORDER BY department, sale_date;

What does the frame clause ROWS BETWEEN 1 PRECEDING AND CURRENT ROW do in this context?
AIt sums the current row's amount and the next row's amount within the department partition.
BIt sums all amounts from the start of the department partition up to the current row.
CIt sums the current row's amount and the amount from the immediately previous sale_date within the same department.
DIt sums only the current row's amount, ignoring other rows.
Attempts:
2 left
💡 Hint
Think about how the frame defines which rows are included relative to the current row.