Challenge - 5 Problems
ROW_NUMBER Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this ROW_NUMBER query?
Consider the table Employees with columns Department and Salary. What will be the
row_num values after running this query?SELECT Department, Salary, ROW_NUMBER() OVER (PARTITION BY Department ORDER BY Salary DESC) AS row_num FROM Employees;
SQL
CREATE TABLE Employees (Department VARCHAR(20), Salary INT); INSERT INTO Employees VALUES ('Sales', 5000), ('Sales', 7000), ('HR', 4000), ('HR', 4500), ('IT', 6000); SELECT Department, Salary, ROW_NUMBER() OVER (PARTITION BY Department ORDER BY Salary DESC) AS row_num FROM Employees;
Attempts:
2 left
💡 Hint
ROW_NUMBER() assigns unique numbers starting at 1 within each partition ordered by Salary descending.
✗ Incorrect
The ROW_NUMBER() function restarts numbering for each Department (partition). It orders salaries descending, so highest salary gets row_num 1, next gets 2, and so on.
🧠 Conceptual
intermediate1:30remaining
What does the PARTITION BY clause do in ROW_NUMBER()?
In the ROW_NUMBER() function, what is the role of the PARTITION BY clause?
Attempts:
2 left
💡 Hint
Think about how numbering restarts for each department in a company.
✗ Incorrect
PARTITION BY splits the data into groups. ROW_NUMBER() then numbers rows starting at 1 within each group separately.
📝 Syntax
advanced2:00remaining
Which query correctly uses ROW_NUMBER() to rank employees by salary?
Choose the query that correctly assigns a row number ranking employees by salary descending without partitioning.
Attempts:
2 left
💡 Hint
ROW_NUMBER() requires OVER() with ORDER BY inside parentheses.
✗ Incorrect
Option B uses correct syntax: ROW_NUMBER() OVER (ORDER BY Salary DESC). Others have syntax errors or misplaced clauses.
❓ optimization
advanced2:30remaining
How to optimize a query using ROW_NUMBER() to get top 3 salaries per department?
Given a large Employees table, which query efficiently returns only the top 3 salaries per department using ROW_NUMBER()?
Attempts:
2 left
💡 Hint
You need to filter after assigning row numbers in a subquery.
✗ Incorrect
Option A assigns row numbers per department and filters for rn <= 3 in an outer query. Others either misuse ROW_NUMBER() or don't partition correctly.
🔧 Debug
expert2:00remaining
Why does this query cause an error?
Given the query:
Why does it cause an error?
SELECT Name, ROW_NUMBER() OVER (PARTITION BY Department) FROM Employees;
Why does it cause an error?
Attempts:
2 left
💡 Hint
ROW_NUMBER() needs to know how to order rows within each partition.
✗ Incorrect
ROW_NUMBER() requires an ORDER BY clause inside OVER() to define the order of numbering. Missing ORDER BY causes syntax error.