Bird
Raised Fist0
SQLquery~20 mins

OVER clause with PARTITION BY in SQL - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does the PARTITION BY clause do when used with the OVER clause in SQL?
easy
A. It divides the result set into groups to perform calculations within each group.
B. It filters rows before aggregation happens.
C. It sorts the entire result set globally.
D. It removes duplicate rows from the result.

Solution

  1. Step 1: Understand the role of PARTITION BY

    The PARTITION BY clause groups rows based on column values, similar to grouping in real life like sorting students by class.
  2. Step 2: See how OVER uses PARTITION BY

    The OVER clause applies functions like SUM or ROW_NUMBER within each group separately, not on the whole table.
  3. Final Answer:

    It divides the result set into groups to perform calculations within each group. -> Option A
  4. Quick Check:

    PARTITION BY = group rows for calculations [OK]
Hint: Think of PARTITION BY as grouping rows inside a window function [OK]
Common Mistakes:
  • Confusing PARTITION BY with WHERE filtering
  • Thinking PARTITION BY sorts all rows globally
  • Assuming PARTITION BY removes duplicates
2. Which of the following is the correct syntax to calculate a running total of sales per department using OVER with PARTITION BY?
easy
A. SELECT department, sales, SUM(sales) OVER ORDER BY date PARTITION BY department AS running_total FROM sales_data;
B. SELECT department, sales, SUM(sales) OVER (PARTITION BY department ORDER BY date) AS running_total FROM sales_data;
C. SELECT department, sales, SUM(sales) PARTITION BY department OVER ORDER BY date AS running_total FROM sales_data;
D. SELECT department, sales, SUM(sales) OVER (ORDER BY date PARTITION BY department) AS running_total FROM sales_data;

Solution

  1. Step 1: Identify correct OVER clause order

    The correct syntax places PARTITION BY before ORDER BY inside the parentheses after OVER.
  2. Step 2: Check each option's syntax

    SELECT department, sales, SUM(sales) OVER (PARTITION BY department ORDER BY date) AS running_total FROM sales_data; correctly uses SUM(sales) OVER (PARTITION BY department ORDER BY date). Others misplace PARTITION BY or OVER keywords.
  3. Final Answer:

    SELECT department, sales, SUM(sales) OVER (PARTITION BY department ORDER BY date) AS running_total FROM sales_data; -> Option B
  4. Quick Check:

    PARTITION BY before ORDER BY inside OVER [OK]
Hint: Remember: OVER (PARTITION BY ... ORDER BY ...) is the correct order [OK]
Common Mistakes:
  • Swapping PARTITION BY and ORDER BY order
  • Placing PARTITION BY outside OVER parentheses
  • Using PARTITION BY without OVER
3. Given the table Employees with columns Department and Salary, what will the following query return?
SELECT Department, Salary, RANK() OVER (PARTITION BY Department ORDER BY Salary DESC) AS SalaryRank FROM Employees;
medium
A. Returns the salary and department without any ranking.
B. Ranks salaries across all employees ignoring departments.
C. Calculates the total salary per department.
D. Ranks salaries within each department from highest to lowest, starting at 1 for the highest salary.

Solution

  1. Step 1: Understand RANK() with PARTITION BY

    The RANK() function assigns ranks starting at 1 within each partition (department), ordered by salary descending.
  2. Step 2: Analyze the query output

    Each employee gets a rank based on salary within their department, highest salary ranked 1, ties get same rank.
  3. Final Answer:

    Ranks salaries within each department from highest to lowest, starting at 1 for the highest salary. -> Option D
  4. Quick Check:

    RANK() OVER PARTITION BY groups and ranks rows [OK]
Hint: RANK() with PARTITION BY ranks within groups, not whole table [OK]
Common Mistakes:
  • Thinking RANK() ignores PARTITION BY
  • Confusing RANK() with SUM()
  • Assuming ranking is global, not per group
4. Identify the error in this SQL query:
SELECT employee_id, department, SUM(salary) OVER PARTITION BY department FROM employees;
medium
A. department column cannot be used in PARTITION BY.
B. SUM() cannot be used with OVER clause.
C. Missing parentheses around PARTITION BY clause inside OVER.
D. employee_id must be included in PARTITION BY.

Solution

  1. Step 1: Check OVER clause syntax

    The OVER clause must have parentheses enclosing PARTITION BY and other clauses.
  2. Step 2: Identify missing parentheses

    The query misses parentheses after OVER, it should be OVER (PARTITION BY department).
  3. Final Answer:

    Missing parentheses around PARTITION BY clause inside OVER. -> Option C
  4. Quick Check:

    OVER requires parentheses around PARTITION BY [OK]
Hint: Always put parentheses after OVER, even if only PARTITION BY used [OK]
Common Mistakes:
  • Omitting parentheses after OVER
  • Using aggregate without OVER for window function
  • Including wrong columns in PARTITION BY
5. You want to find each student's score rank within their class and also show the average score of their class. Which SQL query correctly uses OVER with PARTITION BY to achieve this?
hard
A. SELECT student_id, class, score, RANK() OVER (PARTITION BY class ORDER BY score DESC) AS rank_in_class, AVG(score) OVER (PARTITION BY class) AS avg_class_score FROM student_scores;
B. SELECT student_id, class, score, RANK() OVER (ORDER BY score DESC PARTITION BY class) AS rank_in_class, AVG(score) OVER (PARTITION BY class) AS avg_class_score FROM student_scores;
C. SELECT student_id, class, score, RANK() PARTITION BY class ORDER BY score DESC AS rank_in_class, AVG(score) OVER (class) AS avg_class_score FROM student_scores;
D. SELECT student_id, class, score, RANK() OVER (PARTITION BY class ORDER BY score) AS rank_in_class, AVG(score) OVER (PARTITION BY class ORDER BY score) AS avg_class_score FROM student_scores;

Solution

  1. Step 1: Use correct syntax for RANK() and AVG() with PARTITION BY

    Both functions must have OVER (PARTITION BY class ...). RANK() orders descending for highest score first; AVG() just partitions by class.
  2. Step 2: Check each option's correctness

    SELECT student_id, class, score, RANK() OVER (PARTITION BY class ORDER BY score DESC) AS rank_in_class, AVG(score) OVER (PARTITION BY class) AS avg_class_score FROM student_scores; correctly uses RANK() OVER (PARTITION BY class ORDER BY score DESC) and AVG(score) OVER (PARTITION BY class). Others have syntax errors or wrong order.
  3. Final Answer:

    SELECT student_id, class, score, RANK() OVER (PARTITION BY class ORDER BY score DESC) AS rank_in_class, AVG(score) OVER (PARTITION BY class) AS avg_class_score FROM student_scores; -> Option A
  4. Quick Check:

    Use OVER (PARTITION BY ...) with correct order and parentheses [OK]
Hint: Use separate OVER clauses with PARTITION BY for each window function [OK]
Common Mistakes:
  • Mixing ORDER BY and PARTITION BY order inside OVER
  • Missing parentheses around PARTITION BY
  • Using PARTITION BY without OVER
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard