How can we return only the single highest-paid employee in each department?
Filtering a window-ranked subquery for rn = 1 isolates the top-ranked member of each group.
Department lead identification, highest sale per customer, most recent event extraction.
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;Practice typing production-grade SQL code for Top Employee per Department.