Every language
1 langages, copy-ready. One at a time with syntax highlighting, or all inline.
SQLSQLrunnable
-- Top 3 earners per department: rank inside each group, keep rn <= 3.
WITH ranked AS (
SELECT
department,
name,
salary,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rn
FROM employees
)
SELECT department, name, salary
FROM ranked
WHERE rn <= 3
ORDER BY department, salary DESC;Runs as-is in the playground's SQLite (window functions need SQLite 3.25+). RANK() keeps ties — two rows share rank 1 and both survive an rn <= N filter — while ROW_NUMBER() always breaks ties arbitrarily.