0
0
MySQLquery~20 mins

Window functions (ROW_NUMBER) in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ROW_NUMBER Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of ROW_NUMBER() with PARTITION BY
Given the table Employees with columns Department and Salary, what is the output of the following query?
SELECT Department, Salary, ROW_NUMBER() OVER (PARTITION BY Department ORDER BY Salary DESC) AS rn FROM Employees;
MySQL
CREATE TABLE Employees (Department VARCHAR(20), Salary INT);
INSERT INTO Employees VALUES
('Sales', 5000),
('Sales', 7000),
('HR', 4500),
('HR', 4500),
('IT', 6000);
A[{"Department": "Sales", "Salary": 7000, "rn": 2}, {"Department": "Sales", "Salary": 5000, "rn": 1}, {"Department": "HR", "Salary": 4500, "rn": 2}, {"Department": "HR", "Salary": 4500, "rn": 1}, {"Department": "IT", "Salary": 6000, "rn": 1}]
B[{"Department": "Sales", "Salary": 7000, "rn": 1}, {"Department": "Sales", "Salary": 5000, "rn": 1}, {"Department": "HR", "Salary": 4500, "rn": 1}, {"Department": "HR", "Salary": 4500, "rn": 1}, {"Department": "IT", "Salary": 6000, "rn": 1}]
C[{"Department": "Sales", "Salary": 7000, "rn": 1}, {"Department": "Sales", "Salary": 5000, "rn": 2}, {"Department": "HR", "Salary": 4500, "rn": 1}, {"Department": "HR", "Salary": 4500, "rn": 2}, {"Department": "IT", "Salary": 6000, "rn": 1}]
D[{"Department": "Sales", "Salary": 5000, "rn": 1}, {"Department": "Sales", "Salary": 7000, "rn": 2}, {"Department": "HR", "Salary": 4500, "rn": 1}, {"Department": "HR", "Salary": 4500, "rn": 2}, {"Department": "IT", "Salary": 6000, "rn": 1}]
Attempts:
2 left
💡 Hint
Remember that ROW_NUMBER() resets for each partition and orders rows by Salary descending.
🧠 Conceptual
intermediate
1:30remaining
Understanding ROW_NUMBER() without PARTITION BY
What will be the result of the following query on the Orders table?
SELECT OrderID, CustomerID, ROW_NUMBER() OVER (ORDER BY OrderDate) AS rn FROM Orders;

Assuming Orders has multiple rows with different OrderDate values.
AEach row gets a unique sequential number based on ascending OrderDate across the entire table.
BROW_NUMBER() restarts numbering for each CustomerID.
CAll rows get the same row number because no PARTITION BY is used.
DThe query will cause a syntax error because PARTITION BY is missing.
Attempts:
2 left
💡 Hint
Think about how ROW_NUMBER() works when PARTITION BY is omitted.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in ROW_NUMBER() usage
Which option contains a syntax error in using ROW_NUMBER() in MySQL?
MySQL
SELECT EmployeeID, ROW_NUMBER() OVER (PARTITION EmployeeID ORDER BY Salary) AS rn FROM Employees;
ASELECT EmployeeID, ROW_NUMBER() OVER (PARTITION BY EmployeeID ORDER BY Salary) AS rn FROM Employees;
BSELECT EmployeeID, ROW_NUMBER() OVER (ORDER BY Salary) AS rn FROM Employees;
CSELECT EmployeeID, ROW_NUMBER() OVER () AS rn FROM Employees;
DSELECT EmployeeID, ROW_NUMBER() OVER (PARTITION EmployeeID ORDER BY Salary) AS rn FROM Employees;
Attempts:
2 left
💡 Hint
Check the syntax of PARTITION BY clause in window functions.
optimization
advanced
2:00remaining
Optimizing ROW_NUMBER() query for large datasets
You have a large Sales table and want to assign row numbers partitioned by Region ordered by SaleDate. Which approach is best for performance?
AUse ROW_NUMBER() OVER (PARTITION BY Region ORDER BY SaleDate) without any indexes.
BCreate an index on (Region, SaleDate) and then use ROW_NUMBER() OVER (PARTITION BY Region ORDER BY SaleDate).
CUse a subquery with correlated subselect counting rows per Region and SaleDate instead of ROW_NUMBER().
DUse ROW_NUMBER() OVER (ORDER BY SaleDate) without partitioning.
Attempts:
2 left
💡 Hint
Indexes can speed up sorting and partitioning in window functions.
🔧 Debug
expert
2:30remaining
Debugging unexpected ROW_NUMBER() results with ties
You run this query:
SELECT ProductID, Price, ROW_NUMBER() OVER (PARTITION BY Category ORDER BY Price) AS rn FROM Products;

But you notice that products with the same Price in the same Category get different row numbers. Which is the cause?
AROW_NUMBER() always assigns unique numbers even if values are tied, so ties get different row numbers.
BThe ORDER BY clause is missing a secondary column to break ties, causing random order.
CYou should use RANK() instead of ROW_NUMBER() to assign the same number to ties.
DROW_NUMBER() cannot be used with PARTITION BY and ORDER BY together.
Attempts:
2 left
💡 Hint
Think about how ROW_NUMBER() handles ties compared to RANK().