Module 5 · Advanced queries

Number rows with ROW_NUMBER

By the end you will be able to number rows and start again in each group.

About 14 min

0 of 52 lessons

Before you start, you need

Break queries into steps with CTEs · Sort and limit results

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.

Minimal syntax
ROW_NUMBER() OVER (
  PARTITION BY department
  ORDER BY salary DESC, id
) AS position

See it in action

What position does each Marketing and Sales employee get, starting from the highest salary in their department?

Sample data: 5 rows
namedepartmentsalary
Graham NewtonMarketing31500
Helen CooperMarketing30000
Isaac DavisSales36000
Alex ReedSales33000
Beth MorganSales28000
SQL
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;
Example result: 5 rows
namedepartmentposition
Graham NewtonMarketing1
Helen CooperMarketing2
Isaac DavisSales1
Alex ReedSales2
Beth MorganSales3
  1. The filter selects Marketing and Sales. PARTITION BY department creates one list per department, each starting at position 1.
  2. Inside OVER, salary DESC, id decides the numbering order. Ismael comes first in Sales, followed by Alex and Bea. The id would settle a tie.
  3. 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

Not started

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.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Then restart for each group

Not started

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.

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.

Next: Add sums and averages with OVER