Thursday, January 3, 2013

Find n'th Highest value From Column in Sql Server

N'th Highest Value from column in sql server using subquery;

Suppose we have one table name  Employee  and its two column :Id, Salary

Now if  we want to find 5'th highest salary  of emploee then query is:


SELECT     TOP (1) Id
FROM         (SELECT     TOP (5) Salary
                       FROM          Employee 
                       ORDER BY Salary DESC) AS Salary
ORDER BY Salary


1 comment:

  1. SELECT MAX(Salary) as 'Salary' from EmployeeDetails
    where Salary NOT IN
    (
    SELECT TOP 2 (SALARY) from EmployeeDetails ORDER BY Salary Desc
    )

    ReplyDelete