Skip to main content
SQL • LESSON 141

ROW_NUMBER

How can we assign sequential ranks to sorted employees without collapsing rows?

Advanced3 Minutes1540 XP
🤔 THE QUESTION

How can we assign sequential ranks to sorted employees without collapsing rows?

💡 WHAT IS IT?

ROW_NUMBER() OVER (ORDER BY ...) assigns a unique incrementing integer to each row in the window.

🎯 WHAT IS IT USED FOR?

Pagination, top-N leaderboards, assigning deterministic row indices.

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

🎯 Mission Objectives

Practice typing production-grade SQL code for ROW_NUMBER.

  • ROW_NUMBER()
  • OVER (ORDER BY ...)
  • Sequential ranking
  • Window fundamentals