cswork / libraryOpen practice workspace

Coding questions · TikTok

Find top-paid employee per department

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_id INT
  • employee_name VARCHAR
  • department_id INT
  • department_name VARCHAR
  • salary NUMERIC
  • effective_date DATE (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_name
  • employee_id, employee_name
  • salary

Follow-ups

  1. If an employee can have records in multiple departments, does that affect the result? Explain.
  2. What would affect the result (e.g., ties, duplicates, history rows)?
  3. 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)?

Top-paid employee(s) per department (allow ties)

You are given employee-to-department compensation records. An employee may appear in multiple departments (one row per employee per department).

Write a SQL query to return the employee(s) with the highest salary in each department. If multiple employees tie for the top salary within a department, return all of them.

Return columns: department_id, department_name, employee_id, employee_name, salary.

Table departments

columntypeconstraints
department_idINTPRIMARY KEY, NOT NULL
department_nameVARCHAR(50)NOT NULL

Sample data:

department_iddepartment_name
10Engineering
20Sales
30HR

Table employees

columntypeconstraints
employee_idINTPRIMARY KEY, NOT NULL
employee_nameVARCHAR(50)NOT NULL

Sample data:

employee_idemployee_name
1Alice
2Bob
3Cathy
4Dan
5Eve

Table employee_department_comp

columntypeconstraints
record_idINTPRIMARY KEY, NOT NULL
employee_idINTNOT NULL
department_idINTNOT NULL
salaryDECIMAL(12,2)NOT NULL

Sample data:

department_idemployee_idrecord_idsalary
1011120000
1022120000
103390000
2014110000
2045125000
2036130000
305795000
302897000
304997000

Expected output

One row per (department, top-paid employee). Ties are included, so a department may have multiple rows if multiple employees share the top salary.

department_iddepartment_nameemployee_idemployee_namesalary
10Engineering1Alice120000
10Engineering2Bob120000
20Sales3Cathy130000
30HR2Bob97000
30HR4Dan97000

Hints

  1. Use a window function like DENSE_RANK() partitioned by department.
  2. Filter to rank = 1 to keep the top salary per department; DENSE_RANK keeps ties.

Top-paid employee per department (exactly one row per department)

Using the same tables, return exactly one top-paid employee per department.

If multiple employees tie for the highest salary within a department, break ties by choosing the smallest employee_id.

Return columns: department_id, department_name, employee_id, employee_name, salary.

Table departments

columntypeconstraints
department_idINTPRIMARY KEY, NOT NULL
department_nameVARCHAR(50)NOT NULL

Sample data:

department_iddepartment_name
10Engineering
20Sales
30HR

Table employees

columntypeconstraints
employee_idINTPRIMARY KEY, NOT NULL
employee_nameVARCHAR(50)NOT NULL

Sample data:

employee_idemployee_name
1Alice
2Bob
3Cathy
4Dan
5Eve

Table employee_department_comp

columntypeconstraints
record_idINTPRIMARY KEY, NOT NULL
employee_idINTNOT NULL
department_idINTNOT NULL
salaryDECIMAL(12,2)NOT NULL

Sample data:

department_idemployee_idrecord_idsalary
1011120000
1022120000
103390000
2014110000
2045125000
2036130000
305795000
302897000
304997000

Expected output

Exactly one row per department. When multiple employees tie for top salary, the smallest employee_id is chosen.

department_iddepartment_nameemployee_idemployee_namesalary
10Engineering1Alice120000
20Sales3Cathy130000
30HR2Bob97000

Hints

  1. ROW_NUMBER() (not DENSE_RANK) will force one row per department.
  2. Add a deterministic tie-breaker (e.g., employee_id) to the ORDER BY inside the window function.