---
title: "write a query to n-th highest salary."  
description: "write a query to n-th highest salary."  
author: "Ravi Vishwakarma"  
published: 2024-07-03  
updated: 2024-07-03  
canonical: https://www.mindstick.com/interview/33937/write-a-query-to-n-th-highest-salary  
category: "SQL Server"  
tags: ["database", "sql server", "sql server 2012", "sql injection"]  
reading_time: 3 minutes  

---

# write a query to n-th highest salary.

To retrieve the n-th highest salary from a SQL Server table, you can use a similar approach to the previous query, but adjust the `ROW_NUMBER()` function and filtering criteria. Here's how you can write a query to get the n-th highest salary:

```plaintext
DECLARE @N INT = 3; -- Change this value to the desired N-th highest salary

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

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 `@N`. `@N` is a parameter that you can adjust to retrieve the desired n-th highest salary. In the example above, `@N` is set to `3`, so it will retrieve the third-highest salary.

Adjust the `@N` variable to fetch the n-th highest salary you're interested in from your table. This query will effectively fetch the specified n-th highest salary based on the sorting of the `Salary` column in descending order.

## Read more

[**Write a query to get the second-highest salary.**](https://www.mindstick.com/interview/33936/write-a-query-to-get-the-second-highest-salary)

## Answers

### Answer by Ravi Vishwakarma

To retrieve the n-th highest salary from a SQL Server table, you can use a similar approach to the previous query, but adjust the `ROW_NUMBER()` function and filtering criteria. Here's how you can write a query to get the n-th highest salary:

```plaintext
DECLARE @N INT = 3; -- Change this value to the desired N-th highest salary

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

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 `@N`. `@N` is a parameter that you can adjust to retrieve the desired n-th highest salary. In the example above, `@N` is set to `3`, so it will retrieve the third-highest salary.

Adjust the `@N` variable to fetch the n-th highest salary you're interested in from your table. This query will effectively fetch the specified n-th highest salary based on the sorting of the `Salary` column in descending order.

## Read more

[**Write a query to get the second-highest salary.**](https://www.mindstick.com/interview/33936/write-a-query-to-get-the-second-highest-salary)


---

Original Source: https://www.mindstick.com/interview/33937/write-a-query-to-n-th-highest-salary

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
