Interview Questions/SQL/Highest Paid Employee Per Department

Highest Paid Employee Per Department

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Description

You are given a table of employees. Each employee has a name, a department, a salary, and a hire date. Write a SQL query to find the highest-paid employee(s) in each department. If multiple employees share the same maximum salary in a department, return all of them. Return the result ordered by department, then name. Table: Employees

Column NameTypeDescription
idINTPrimary key
nameVARCHAREmployee name
departmentVARCHARDepartment name
salaryINTAnnual salary
hire_dateDATEDate employee was hired

Database Schema (Inferred)

Employees

Column NameExample Value
id1
nameAlice
departmentEngineering
salary90000
hire_date2020-01-15

Example

Employees

idnamedepartmentsalaryhire_date
1AliceEngineering900002020-01-15
2BobEngineering850002019-03-10
3CarolMarketing700002021-06-01
4DaveMarketing720002020-09-20
5EveHR600002022-02-28

Output

departmentnamesalary
EngineeringAlice90000
HREve60000
MarketingDave72000

Explanation:

For each department, find the employee(s) whose salary equals the maximum salary in that department.

Approach hint

Start with a simple approach, explain the trade-off, then move toward a cleaner or more scalable solution.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

SQL Editor
Loading...

Employees

idnamedepartmentsalaryhire_date
1AliceEngineering900002020-01-15
2BobEngineering850002019-03-10
3CarolMarketing700002021-06-01
4DaveMarketing720002020-09-20
5EveHR600002022-02-28

Output

departmentnamesalary
EngineeringAlice90000
HREve60000
MarketingDave72000