⌨ Programming Hard SQL Joins

Highest-Paid Employee Per Department

Question

Find the highest-paid employee in each department. Return department, employee full name, role, and salary.

πŸ’‘ Use the employees table. You will need a subquery or window function.
SQL Editor
Solution
SELECT
  e.department,
  e.first_name || ' ' || e.last_name AS employee_name,
  e.role,
  e.salary
FROM employees e
JOIN (
  SELECT department, MAX(salary) AS max_salary
  FROM employees
  GROUP BY department
) top ON e.department = top.department AND e.salary = top.max_salary
ORDER BY e.salary DESC;
Explanation: Subquery finds MAX(salary) per department. The outer query JOINs back to get the full employee row matching that maximum.