Skip to main content
SQL โ€ข LESSON 150

Top Employee per Department

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

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