๐ Find the top 3 highest-paid employees from each department
Table: Employees
employee_id | employee_name
| department_id | salary
๐ Query:
WITH ranked AS (
SELECT employee_id,
employee_name,
department_id,
salary,
DENSE_RANK() OVER (
PARTITION BY department_id
ORDER BY salary DESC
) AS rnk
FROM Employees
)
SELECT *
FROM ranked
WHERE rnk <= 3;
๐ฏ Why this question matters:
โ Tests window functions (
DENSE_RANK)โ Evaluates partitioning concepts
โ Checks top-N problem-solving skills
โ Frequently asked in advanced SQL interviews
๐ Pro Tip:
Use
DENSE_RANK() instead of ROW_NUMBER() when you want to handle salary ties correctly.๐ฅ Top-N per group questions are extremely popular in Data Analyst interviews.
โค๏ธ React for more advanced SQL interview questions