---
title: "Write a query to get the second-highest salary."  
description: "Write a query to get the second-highest salary."  
author: "Ravi Vishwakarma"  
published: 2024-07-03  
updated: 2024-07-03  
canonical: https://www.mindstick.com/interview/33936/write-a-query-to-get-the-second-highest-salary  
category: "SQL Server"  
tags: ["database", "sql server", "database schema"]  
reading_time: 2 minutes  

---

# Write a query to get the second-highest salary.

To retrieve the second-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.

## 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)

## Answers

### Answer by Ravi Vishwakarma

To retrieve the second-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.

## 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)


---

Original Source: https://www.mindstick.com/interview/33936/write-a-query-to-get-the-second-highest-salary

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
