Challenge - 5 Problems
ROW_NUMBER Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2: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);
Attempts:
2 left
💡 Hint
Remember that ROW_NUMBER() resets for each partition and orders rows by Salary descending.
✗ Incorrect
ROW_NUMBER() assigns a unique sequential number to rows within each Department partition, ordered by Salary descending. So the highest salary in each department gets rn = 1, the next rn = 2, and so on.
🧠 Conceptual
intermediate1:30remaining
Understanding ROW_NUMBER() without PARTITION BY
What will be the result of the following query on the Orders table?
Assuming
SELECT OrderID, CustomerID, ROW_NUMBER() OVER (ORDER BY OrderDate) AS rn FROM Orders;
Assuming
Orders has multiple rows with different OrderDate values.Attempts:
2 left
💡 Hint
Think about how ROW_NUMBER() works when PARTITION BY is omitted.
✗ Incorrect
Without PARTITION BY, ROW_NUMBER() numbers all rows in the entire result set ordered by OrderDate. It does not restart numbering for any group.
📝 Syntax
advanced1: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;
Attempts:
2 left
💡 Hint
Check the syntax of PARTITION BY clause in window functions.
✗ Incorrect
The correct syntax requires PARTITION BY keyword. Option D misses the BY keyword causing a syntax error.
❓ optimization
advanced2: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?Attempts:
2 left
💡 Hint
Indexes can speed up sorting and partitioning in window functions.
✗ Incorrect
Creating an index on the partition and order columns helps the database efficiently compute ROW_NUMBER() by reducing sorting cost.
🔧 Debug
expert2:30remaining
Debugging unexpected ROW_NUMBER() results with ties
You run this query:
But you notice that products with the same Price in the same Category get different row numbers. Which is the cause?
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?
Attempts:
2 left
💡 Hint
Think about how ROW_NUMBER() handles ties compared to RANK().
✗ Incorrect
ROW_NUMBER() always gives unique sequential numbers, so even tied rows get different numbers. To assign the same number to ties, use RANK() or DENSE_RANK().