Before you start, you need
Number the rows without losing the detail
To rank employees by salary, you want to keep their names and add a position: 1, 2, 3… ROW_NUMBER numbers the rows without turning them into a summary.
OVER says how to calculate the numbering. Inside it, ORDER BY salary DESC, id puts the highest salary first and uses the id to settle a tie. The number is added to each employee as a new column.
PARTITION BY creates separate lists. With PARTITION BY department, Sales and Marketing each get their own position 1. Those lists are called partitions. Without that part, every employee is numbered in one single list.
There are two different orderings: the ORDER BY within OVER inside decides the position; the outer one decides how the result is presented. To show only position 1, compute the numbers in a CTE first and filter in the query that reads it.
ROW_NUMBER() OVER (
PARTITION BY department
ORDER BY salary DESC, id
) AS positionSee it in action
What position does each Marketing and Sales employee get, starting from the highest salary in their department?
| name | department | salary |
|---|---|---|
| Graham Newton | Marketing | 31500 |
| Helen Cooper | Marketing | 30000 |
| Isaac Davis | Sales | 36000 |
| Alex Reed | Sales | 33000 |
| Beth Morgan | Sales | 28000 |
SELECT name,
department,
ROW_NUMBER() OVER (
PARTITION BY department
ORDER BY salary DESC, id
) AS position
FROM employees
WHERE department IN ('Marketing', 'Sales')
ORDER BY department, position;| name | department | position |
|---|---|---|
| Graham Newton | Marketing | 1 |
| Helen Cooper | Marketing | 2 |
| Isaac Davis | Sales | 1 |
| Alex Reed | Sales | 2 |
| Beth Morgan | Sales | 3 |
- The filter selects Marketing and Sales.
PARTITION BY departmentcreates one list per department, each starting at position 1. - Inside OVER,
salary DESC, iddecides the numbering order. Ismael comes first in Sales, followed by Alex and Bea. The id would settle a tie. - The outer ORDER BY shows each department first and then its positions. If you wanted to keep only position 1, you would compute the numbers in a CTE and filter in the outer query.
Practice with help
Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.
Guided practice
First, one numbering across all rows
Show id, name and ROW_NUMBER as position, numbering employees by salary descending and id ascending. Sort the output by position.
Expected result: 10 rows with the columns id, name, position, in that order. Keep the row order requested.
View tables and data
Open this panel to see the full data.
In this first activity, every employee shares one list.
Number from the highest salary down and use id to break ties.
The numbering order goes inside OVER; the output is sorted by position.
Solution explained
SELECT id, name, ROW_NUMBER() OVER (ORDER BY salary DESC, id) AS position FROM employees ORDER BY position;A single numbering keeps every employee and fixes a unique order.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Then restart for each group
Show name, department and ROW_NUMBER as position. Restart for each department and number by salary descending and id ascending. Sort by department and position.
Expected result: 10 rows with the columns name, department, position, in that order. Keep the row order requested.
View tables and data
Open this panel to see the full data.
The position must start again in each department.
PARTITION BY defines the groups and ORDER BY the ranking.
Include id as a tiebreaker inside OVER.
Solution explained
SELECT name, department, ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC, id) AS position FROM employees ORDER BY department, position;Each department has its own position 1 without losing individual rows.
Key takeaways
- ROW_NUMBER numbers rows.
- PARTITION BY restarts the numbering for each group.
- The ORDER BY inside OVER defines the position.
- A tiebreaker key makes the positions reproducible.