Use RANK when tied rows should all qualify.
Section: Top N per group
Return top values including ties
sql
sql
WITH ranked AS (
SELECT employee_id,
department_id,
salary,
RANK() OVER (
PARTITION BY department_id
ORDER BY salary DESC
) AS rnk
FROM employees
)
SELECT *
FROM ranked
WHERE rnk <= 3;Explanation
This returns all rows tied within the top ranks, which can exceed 3 rows per group.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing Top N per group