Find top-paid employee per department
TikTokData Scientist · Technical ScreenLast updated
This question evaluates a candidate's competency in SQL data manipulation, including aggregation, ranking, handling of duplicate and historical rows, tie semantics, and temporal data interpretation when determining the top-paid employee per department.
Tables
Assume the company stores employee compensation by department assignment.
employee_dept_salary
employee_idINTemployee_nameVARCHARdepartment_idINTdepartment_nameVARCHARsalaryNUMERICeffective_dateDATE (optional; if present, assume you want the most recent salary)
Notes:
- An employee may appear in multiple departments (e.g., matrix org or multiple assignments).
- There may be multiple rows per
(employee_id, department_id)(e.g., historical changes) unless otherwise stated.
Task
Write a SQL query to return the top 1 highest-paid employee per department.
Output
For each department, return:
department_id,department_nameemployee_id,employee_namesalary
Follow-up questions
- If an employee can have records in multiple departments, does that affect the result? Explain.
- What *would* affect the result (e.g., ties, duplicates, history rows)?
- If the query can return multiple employees due to ties, but you **only want one row per department**, how would you enforce that (define a deterministic tie-break)?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.