Skip to main content
SQL • LESSON 142

ROW_NUMBER by Department

How can we rank employees within each department independently?

Advanced3 Minutes1550 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