---
title: "How to Write a Query to Find the Second Highest Salary in SQL Server?"  
description: "How to Write a Query to Find the Second Highest Salary in SQL Server?"  
author: "Anubhav Sharma"  
published: 2024-07-15  
updated: 2024-07-16  
canonical: https://www.mindstick.com/forum/160917/how-to-write-a-query-to-find-the-second-highest-salary-in-sql-server  
category: "SQL Server"  
tags: ["database", "sql server", "sql server 2008", "sql server 2012", "sql server 2022"]  
reading_time: 2 minutes  

---

# How to Write a Query to Find the Second Highest Salary in SQL Server?

How to Write a Query to Find the [Second Highest](https://www.mindstick.com/interview/23307/how-to-find-second-highest-salary-from-table) Salary in SQL Server?

## Replies

### Reply by Ravi Vishwakarma

To retrieve the [**second-highest salary**](https://www.mindstick.com/interview/33937/write-a-query-to-n-th-highest-salary) from a SQL Server table, you can use a subquery with the `ROW_NUMBER()` function. Here's how you can do it:

```plaintext
SELECT TOP 1 Salary
FROM (
    SELECT Salary, ROW_NUMBER() OVER (ORDER BY Salary DESC) AS RowNum
    FROM YourTableName
) AS SalaryRanked
WHERE RowNum = 2;
```

In this query:

**Inner Query (**`SalaryRanked`**)**: This subquery selects the `Salary` column from your table (`YourTableName`) and assigns a row number (`RowNum`) to each row based on the descending order of `Salary`.

**Outer Query**: The outer query selects the `Salary` from the `SalaryRanked` subquery where the `RowNum` equals `2`, which corresponds to the second-highest salary.

Make sure to replace `YourTableName` with the actual name of your table and `Salary` with the actual column name containing the salaries in your database schema. This query will give you the second-highest salary from your table.

## Example

```plaintext
Select TOP 1 *
from (
	select *, ROW_NUMBER() OVER(order by Salary Desc ) As RNO
	from Employees
) As EMP
where EMP.RNO = 2
```

#### OR

```plaintext
select top 1 * from Employees
where Salary < (select MAX(Salary) As Salary from Employees)
order by Salary Desc
```

## Read more

[**Write a basic SELECT statement to retrieve data from a SQL Server table.**](https://www.mindstick.com/interview/33935/write-a-basic-select-statement-to-retrieve-data-from-a-sql-server-table)

[**Help with Writing a Subquery to Get Aggregate Data in SQL Server**](https://www.mindstick.com/forum/160910/help-with-writing-a-subquery-to-get-aggregate-data-in-sql-server)

[**SQL Query to Calculate Running Total in SQL Server**](https://www.mindstick.com/forum/160914/sql-query-to-calculate-running-total-in-sql-server)


---

Original Source: https://www.mindstick.com/forum/160917/how-to-write-a-query-to-find-the-second-highest-salary-in-sql-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
