How to retrieve 3rd Max salary from a table in sql server?

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #5150
    Pavan
    Member

    I am facing problem when i m fetching data from data base then i need to 3rd max salary and some times condition changed then we need to fetch nth number salary.I want a generalized query.

    #5151

    Simple use sql query like as:

    SELECT TOP 1 Empsalary
    FROM (
    SELECT DISTINCT TOP 3 Empsalary
    FROM tblemployee
    ORDER BY Empsalary DESC) a
    ORDER BY Empsalary

    You can get nth highest number or salary from the above query.

    #5153
    Ritesh
    Member

    you can also use Sql rank() function like :

    select Empname,Empsalary from (
    select Empname,Empsalary,rank() over (order by Empsalary desc) as ranking from tblemployee
    ) a
    where ranking = 3

    hope this helps….

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.