Skip to main content
SQL โ€ข LESSON 142

ROW_NUMBER by Department

How can we rank employees within each department independently?

Advancedโฑ 3 Minutesโญ 1550 XP
๐Ÿค” THE QUESTION

How can we rank employees within each department independently?

๐Ÿ’ก WHAT IS IT?

PARTITION BY resets the window calculation for each unique department group.

๐ŸŽฏ WHAT IS IT USED FOR?

Top employee per department, local leaderboards, departmental compensation ranks.

๐Ÿ’ป EXAMPLE
SELECT
name,
department_id,
salary,
ROW_NUMBER() OVER (
    PARTITION BY department_id
    ORDER BY salary DESC
) AS department_rank
FROM employees;

๐ŸŽฏ Mission Objectives

Practice typing production-grade SQL code for ROW_NUMBER by Department.

  • โœ“ PARTITION BY
  • โœ“ Partitioned ranking
  • โœ“ Department-level window
  • โœ“ Local rank reset