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
Get the latest row per group
Use ROW_NUMBER in a subquery or CTE.
OpenIn sheetsqlsame section
Get top 3 rows per group
Keep the highest-value rows within each partition.
OpenIn sheetsqlsame section
RANK
Tied rows share rank and leave gaps.
OpenIn sheetsql1 tag match
ROW_NUMBER
Unique sequence with no ties.
DENSE_RANK
Tied rows share rank without gaps.
Split rows into quartiles with NTILE
Bucket ordered rows into four groups.