0
0
SQLquery~20 mins

ROW_NUMBER function in SQL - 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
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;
A[{'Department': 'Sales', 'Salary': 7000, 'row_num': 1}, {'Department': 'Sales', 'Salary': 5000, 'row_num': 2}, {'Department': 'HR', 'Salary': 4500, 'row_num': 1}, {'Department': 'HR', 'Salary': 4000, 'row_num': 2}, {'Department': 'IT', 'Salary': 6000, 'row_num': 1}]
B[{'Department': 'Sales', 'Salary': 5000, 'row_num': 1}, {'Department': 'Sales', 'Salary': 7000, 'row_num': 2}, {'Department': 'HR', 'Salary': 4000, 'row_num': 1}, {'Department': 'HR', 'Salary': 4500, 'row_num': 2}, {'Department': 'IT', 'Salary': 6000, 'row_num': 1}]
C[{'Department': 'Sales', 'Salary': 7000, 'row_num': 2}, {'Department': 'Sales', 'Salary': 5000, 'row_num': 1}, {'Department': 'HR', 'Salary': 4500, 'row_num': 2}, {'Department': 'HR', 'Salary': 4000, 'row_num': 1}, {'Department': 'IT', 'Salary': 6000, 'row_num': 1}]
D[{'Department': 'Sales', 'Salary': 7000, 'row_num': 1}, {'Department': 'Sales', 'Salary': 5000, 'row_num': 1}, {'Department': 'HR', 'Salary': 4500, 'row_num': 1}, {'Department': 'HR', 'Salary': 4000, 'row_num': 1}, {'Department': 'IT', 'Salary': 6000, 'row_num': 1}]
Attempts:
2 left
💡 Hint
ROW_NUMBER() assigns unique numbers starting at 1 within each partition ordered by Salary descending.
🧠 Conceptual
intermediate
1: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?
AIt filters rows before numbering them.
BIt limits the number of rows returned by the query.
CIt orders the entire table before numbering rows.
DIt divides the rows into groups where row numbering restarts for each group.
Attempts:
2 left
💡 Hint
Think about how numbering restarts for each department in a company.
📝 Syntax
advanced
2: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.
ASELECT Name, Salary, ROW_NUMBER() PARTITION BY ORDER BY Salary DESC AS rank FROM Employees;
BSELECT Name, Salary, ROW_NUMBER() OVER (ORDER BY Salary DESC) AS rank FROM Employees;
CSELECT Name, Salary, ROW_NUMBER() OVER (PARTITION Salary ORDER BY DESC Salary) AS rank FROM Employees;
DSELECT Name, Salary, ROW_NUMBER() OVER ORDER BY Salary DESC AS rank FROM Employees;
Attempts:
2 left
💡 Hint
ROW_NUMBER() requires OVER() with ORDER BY inside parentheses.
optimization
advanced
2: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()?
ASELECT * FROM (SELECT Department, Salary, ROW_NUMBER() OVER (PARTITION BY Department ORDER BY Salary DESC) AS rn FROM Employees) sub WHERE rn <= 3;
BSELECT Department, Salary FROM Employees WHERE ROW_NUMBER() OVER (PARTITION BY Department ORDER BY Salary DESC) <= 3;
CSELECT Department, Salary FROM Employees ORDER BY Salary DESC LIMIT 3;
DSELECT Department, Salary, ROW_NUMBER() OVER (ORDER BY Salary DESC) AS rn FROM Employees WHERE rn <= 3;
Attempts:
2 left
💡 Hint
You need to filter after assigning row numbers in a subquery.
🔧 Debug
expert
2:00remaining
Why does this query cause an error?
Given the query:
SELECT Name, ROW_NUMBER() OVER (PARTITION BY Department) FROM Employees;

Why does it cause an error?
ABecause ROW_NUMBER() requires a GROUP BY clause in the main query.
BBecause PARTITION BY cannot be used without ORDER BY.
CBecause ORDER BY clause is missing inside the OVER() clause.
DBecause the Employees table must have a unique ID column for ROW_NUMBER() to work.
Attempts:
2 left
💡 Hint
ROW_NUMBER() needs to know how to order rows within each partition.