Skip to main content
SQL โ€ข LESSON 141

ROW_NUMBER

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

Advancedโฑ 3 Minutesโญ 1540 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