Find Nth Highest Salary of Employee from sql server

Description:-

In this example we explain that how to find nth highest salary of the employee in Sql server table. This is the most important question that is asked by interviewer in any interview that is write query for find second highest salary of employee in Sql table or find third highest salary of employee in Sql server table.

We all listen that How to find third highest or second maximum salary of an Employee is one of the most frequently asked question in interview.
There are many ways to find nth highest salaries of an employee are as below:
--The following solution is for getting 3th highest salary from Employee table ,

SELECTTOP 1 salary
FROM (
SELECTDISTINCTTOP 3 salary
FROM employee
ORDERBY salary DESC) a
ORDERBY salary

--if you want to find 2 highest salary then put 2 instead of 6 if you want to find 4th highest salary then put 4 instead of 6 and so far.
--put the value in place of n that you want to find

SELECTTOP 1 salary
FROM (
SELECTDISTINCTTOP n salary
FROM employee
ORDERBY salary DESC) a
ORDERBY salary

Related Posts

Previous
Next Post »

Thanks for comments.....