You're working as a HR Analytics Specialist for a large corporation and need to identify the top earners in each department to help with budget planning and performance reviews.
You have access to two database tables:
- Employee Table: Contains employee ID, name, salary, and their department ID
- Department Table: Contains department ID and department name
Your task is to write a SQL query that finds all employees who have the highest salary within their respective departments. Note that multiple employees can have the same highest salary in a department, and you need to return all of them.
Example: If the Engineering department has two employees earning $90,000 (the highest in that department), both should be included in the result.
Input & Output
Time & Space Complexity
Single pass with sorting for window function ranking
Space for intermediate ranking results
Constraints
- 1 ⤠Employee table rows ⤠104
- 1 ⤠Department table rows ⤠102
- Employee.departmentId is a foreign key referencing Department.id
- All salary values are positive integers
- Department names are unique and not NULL