Skip to main content
SQL • LESSON 150

Top Employee per Department

How can we return only the single highest-paid employee in each department?

Advanced3 Minutes1630 XP
🤔 THE QUESTION

How can we return only the single highest-paid employee in each department?

💡 WHAT IS IT?

Filtering a window-ranked subquery for rn = 1 isolates the top-ranked member of each group.

🎯 WHAT IS IT USED FOR?

Department lead identification, highest sale per customer, most recent event extraction.

💻 EXAMPLE
SELECT *
FROM (
    SELECT
        name,
        department_id,
        salary,
        ROW_NUMBER() OVER (
            PARTITION BY department_id
            ORDER BY salary DESC
        ) AS rn
    FROM employees
) ranked
WHERE rn = 1;

🎯 Mission Objectives

Practice typing production-grade SQL code for Top Employee per Department.

  • Top 1 per group
  • Window in subquery
  • WHERE rn = 1
  • Deduplication and leadership patterns