Scale customer reach and grow sales with AskHandle chatbot

What are the top three salaries in each department using SQL?

When working with SQL, one common challenge is retrieving the top three highest salaries from each department within a company. This question often comes up in tech interviews and can be effectively tackled using a combination of window functions and common table expressions (CTEs). Below, we’ll explore how to achieve this in a straightforward manner.

image-1
Written by
Published onApril 11, 2025
RSS Feed for BlogRSS Blog

What are the top three salaries in each department using SQL?

When working with SQL, one common challenge is retrieving the top three highest salaries from each department within a company. This question often comes up in tech interviews and can be effectively tackled using a combination of window functions and common table expressions (CTEs). Below, we’ll explore how to achieve this in a straightforward manner.

Let’s assume we have an employees table structured like this:

Sql

To find the top three salaries in each department, we can utilize the ROW_NUMBER() window function. This function allows us to assign a unique sequential integer to rows within a partition of a result set. The basic SQL query to achieve this could look like the following:

Sql

Explanation of the SQL Query

  1. Common Table Expression (CTE): The query begins with a CTE named RankedSalaries. This allows us to create a temporary result set where we assign ranks to the salaries within each department.

  2. ROW_NUMBER() Function: Inside the CTE, we use the ROW_NUMBER() function along with PARTITION BY department. This creates groups (or partitions) based on the department column and orders the salaries in descending order. Each salary within a department gets a rank starting from 1 for the highest salary.

  3. Filtering: After generating the ranked list of salaries, we retrieve only those entries where the rank (salary_rank) is less than or equal to 3. This way, we filter our results to show only the top three salaries for each department.

  4. Final Selection: The final SELECT statement retrieves the relevant columns from our ranked salaries while ensuring the output is ordered first by department and then by salary in descending order.

Alternative Method Using DENSE_RANK()

If there is a chance that two employees in the same department could have the same salary, resulting in more than three employees being tied for the top three salaries, you might want to consider using the DENSE_RANK() function instead:

Sql

Using DENSE_RANK() ensures that if there are ties, the next distinct salary will still get a rank, allowing for a more inclusive set of results if needed.

By mastering this SQL technique, one can efficiently address the common inquiry of retrieving the top salaries within categorized departments, showcasing both analytical skills and SQL proficiency during tech interviews.

Create your AI Agent

Automate customer interactions in just minutes with your own AI Agent.

Featured posts

Subscribe to our newsletter

Achieve more with AI

Enhance your customer experience with an AI Agent today. Easy to set up, it seamlessly integrates into your everyday processes, delivering immediate results.

Latest posts

AskHandle Blog

Ideas, tips, guides, interviews, industry best practices, and news.

View all posts