---
title: "How do you retrieve the second highest salary in SQL Server?"  
description: "How do you retrieve the second highest salary in SQL Server?"  
author: "Anubhav Sharma"  
published: 2026-05-14  
updated: 2026-05-20  
canonical: https://answers.mindstick.com/qa/116621/how-do-you-retrieve-the-second-highest-salary-in-sql-server  
category: "database"  
tags: ["database", "sql server"]  
reading_time: 2 minutes  

---

# How do you retrieve the second highest salary in SQL Server?

## Answers

### Answer by Ravi Vishwakarma

There are several common ways to retrieve the second [highest salary](https://www.mindstick.com/interview/23307/how-to-find-second-highest-salary-from-table) in [SQL Server](https://www.mindstick.com/articles/12343/sql-server-2017-ctp-2-0-now-available). Here are the best approaches:

## 1. Using `MAX()` with a subquery

```plaintext
-- Get the second highest salary
SELECT MAX(Salary) AS SecondHighestSalary
FROM Employees
WHERE Salary < (
    SELECT MAX(Salary)
    FROM Employees
);
```

### How it works

- Inner query finds the highest salary.
- Outer query finds the maximum salary smaller than the highest.

## 2. Using `DENSE_RANK()` (Recommended)

```plaintext
WITH SalaryRanks AS (
    SELECT
        Salary,
        DENSE_RANK() OVER (ORDER BY Salary DESC) AS RankNo
    FROM Employees
)
SELECT Salary
FROM SalaryRanks
WHERE RankNo = 2;
```

### Why this is good

- Handles [duplicate](https://www.mindstick.com/forum/160730/contain-duplicate) salaries correctly.
- Easy to extend for 3rd, 4th, [nth highest](https://www.mindstick.com/forum/33705/find-nth-highest-salary-in-sql-serever) salary.

## 3. Using `ROW_NUMBER()`

```plaintext
WITH SalaryRanks AS (
    SELECT
        Salary,
        ROW_NUMBER() OVER (ORDER BY Salary DESC) AS RowNum
    FROM Employees
)
SELECT Salary
FROM SalaryRanks
WHERE RowNum = 2;
```

### Difference

- `ROW_NUMBER()` gives unique sequence numbers.
- If duplicate salaries exist, this may not return the true second distinct salary.

## Example

Suppose the table contains:

| [Employee](https://www.mindstick.com/articles/85763/7-ways-to-use-big-data-in-employee-training) | Salary |
| --- | --- |
| A | 9000 |
| B | 8000 |
| C | 8000 |
| D | 7000 |

- `DENSE_RANK()` → second highest salary = `8000`
- `ROW_NUMBER()` → second row salary = `8000` (but duplicates affect ranking)

## Best Practice

Use `DENSE_RANK()` when:

- You want the **second distinct highest salary**.
- Duplicate salaries are possible.

Use `MAX()` [approach](https://www.mindstick.com/interview/985/what-are-the-approaches-that-you-will-follow-for-making-a-program-very-efficient) when:

- You need a simple [interview](https://yourviews.mindstick.com/view/83067/how-to-prepare-for-the-star-method-of-interview)-style solution.


---

Original Source: https://answers.mindstick.com/qa/116621/how-do-you-retrieve-the-second-highest-salary-in-sql-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
