Highest Paid Employee Per Department
Preview mode. Log in to edit, run, submit, and save progress.
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 Name | Type | Description |
|---|---|---|
| id | INT | Primary key |
| name | VARCHAR | Employee name |
| department | VARCHAR | Department name |
| salary | INT | Annual salary |
| hire_date | DATE | Date employee was hired |
Database Schema (Inferred)
Employees
| Column Name | Example Value |
|---|---|
| id | 1 |
| name | Alice |
| department | Engineering |
| salary | 90000 |
| hire_date | 2020-01-15 |
Example
Employees
| id | name | department | salary | hire_date |
|---|---|---|---|---|
| 1 | Alice | Engineering | 90000 | 2020-01-15 |
| 2 | Bob | Engineering | 85000 | 2019-03-10 |
| 3 | Carol | Marketing | 70000 | 2021-06-01 |
| 4 | Dave | Marketing | 72000 | 2020-09-20 |
| 5 | Eve | HR | 60000 | 2022-02-28 |
Output
| department | name | salary |
|---|---|---|
| Engineering | Alice | 90000 |
| HR | Eve | 60000 |
| Marketing | Dave | 72000 |
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.
Employees
| id | name | department | salary | hire_date |
|---|---|---|---|---|
| 1 | Alice | Engineering | 90000 | 2020-01-15 |
| 2 | Bob | Engineering | 85000 | 2019-03-10 |
| 3 | Carol | Marketing | 70000 | 2021-06-01 |
| 4 | Dave | Marketing | 72000 | 2020-09-20 |
| 5 | Eve | HR | 60000 | 2022-02-28 |
Output
| department | name | salary |
|---|---|---|
| Engineering | Alice | 90000 |
| HR | Eve | 60000 |
| Marketing | Dave | 72000 |