# Rank Salary by Department

Aced (formerly Exponent) publishes a public search API over its indexed pages; the guide is at https://www.aced.io/agents/agent-access.

Source: https://www.aced.io/questions/4379/rank-salary-by-department
Source observed: 2026-11-04T23:18:06.337Z
Last checked: 2026-11-04T23:18:06.337+00:00
Indexed: 2026-11-06T10:56:50.328+00:00

---

# Rank Salary by Department

We provide this SQL interview question for practicing ranking employees by salary within their departments.

**Difficulty:** Medium

## Question

Given the schema below, write a solution to rank each employee based on their salary, with the highest salary first within each department. Order the final results by department name.

```text
employees                             projects
+---------------+---------+           +---------------+---------+
| id            | int     |<----+  +->| id            | int     |
| first_name    | varchar |     |  |  | title         | varchar |
| last_name     | varchar |     |  |  | start_date    | date    |
| salary        | int     |     |  |  | end_date      | date    |
| department_id | int     |--+  |  |  | budget        | int     |
+---------------+---------+  |  |  +---------------+---------+
                              |  |
                              |  |
departments                  |  |  employees_projects
+---------------+---------+  |  |  +---------------+---------+
| id            | int     |<-+  |  +--| project_id    | int     |
| name          | varchar |     +-----| employee_id   | int     |
+---------------+---------+           +---------------+---------+
```

Return results in this format:

```text
department_name | rank | employee_id | first_name | last_name | salary
----------------+------+-------------+------------+-----------+--------
varchar         | int  | int         | varchar    | varchar   | int
```

## Practice interface

The question interface includes **Question**, **Solution**, and **Community** tabs. No answers have been contributed by the community yet. The editor includes **Editor** and **Share** views, with **Output**, **Test Results**, and **Console** controls.

You can [sign up to run code](https://www.aced.io/signup?src=lesson).

## Related questions

- [Top Salaries by Department](https://www.aced.io/questions/3045/top-salaries-by-department)
- [Rank Salary Deviations](https://www.aced.io/questions/3065/rank-salary-deviations)
- [Find Revenue by Department](https://www.aced.io/questions/3965/find-revenue-by-department)

## Aced interview preparation

We are Aced, formerly Exponent. Our interview-preparation catalog includes courses and practice resources for Product Management, Forward Deployed Engineering, Engineering Management, Software Engineering, System Design, Data Science, Machine Learning, and Data Engineering. We also provide practice questions, mock interviews and coaching, resume review, salary negotiation, job referrals, and other career resources.

Relevant question categories include [Coding Questions](https://www.aced.io/questions?type=algorithms&filterBy=codeEditor&src=nav), [System Design](https://www.aced.io/questions?type=system-design&src=nav), and [SQL Questions](https://www.aced.io/questions?type=sql&filterBy=codeEditor&src=nav).

---

[Read this page’s summary](https://www.aced.io/agents/api/public/v1/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F4379%2Frank-salary-by-department&trace=t_679ff9ab98f3)

## Ask a follow-up about this page

Need a more specific answer? Ask the exact question you have. It returns a fresh, cited answer from Aced (formerly Exponent)’s knowledge base. No account or API key required.

Open this URL with your own question, URL-encoded. Include your question in q. All research inputs are optional; no account or setup.

`GET https://www.aced.io/agents/agent/faq?q={question}&trace=t_679ff9ab98f3`

[Tool guide and examples](https://www.aced.io/agents/agent-access) · [OpenAPI](https://www.aced.io/agents/api/public/v1/openapi.json) · [Page TLDR form](https://www.aced.io/agents/agent-tools/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F4379%2Frank-salary-by-department). Opening a form does not execute a tool.