SQL SERVER – Find Nth /Second/Third Highest Salary – Common interview Question

This is a common question in Interview “find the second highest salary of Employee details table”. I hope most of my friends also have faced this question. Here I am explaining about two different methods-

Using Row_number()
Without using any SQL Function.

This is my Employeedetails table.

Method 1: Using Row_number()

Here I am showing third highest salary.

Method 2 – With no SQL Server Function

Another method: Suggested by Fazal – one of the regular blog reader of SQLINFO.


SELECT MAX(salary)

FROM   employeedetails

WHERE  salary NOT IN (SELECT TOP 1 salary

                      FROM   employeedetails

                      ORDER  BY salary DESC) 

print

7 thoughts on “SQL SERVER – Find Nth /Second/Third Highest Salary – Common interview Question

  1. Hi Varun,

    We can also fetch 2nd Highest Salary from following query:

    select max(salary) from Employeedetails
    where salary not in (select top 1 salary from Employeedetails order by salary desc )

    Thanks & Regards,
    Fazal Vahora

  2. Please tell me the possible watys to get not just the nth max salary but also the details of the employee who is getting the n th highest salary. Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *


four − 1 =