SQL • LESSON 25

ROW_NUMBER()

Learn how to assign a unique sequential number to each row in a query result.

🔴 Advanced⏱ 3 Minutes⭐ 440 XP
🤔 THE QUESTION

How can we give every employee a unique position number based on salary?

💡 WHAT IS IT?

ROW_NUMBER() assigns a unique sequential number to every row in the result.

🎯 WHAT IS IT USED FOR?

It is commonly used for ranking rows, removing duplicates, selecting the first or latest record, and creating ordered row numbers.

💻 EXAMPLE
SELECT name,
salary,
ROW_NUMBER() OVER (
  ORDER BY salary DESC
) AS salary_rank
FROM employees;

Mission Brief

Practice using ROW_NUMBER() to rank employees by salary from highest to lowest.

Objectives

  • SELECT
  • ROW_NUMBER()
  • OVER
  • ORDER BY
  • Window functions