SQL query to find Nth highest salary

前端 未结 9 801
清酒与你
清酒与你 2020-12-15 01:52

I am referring to following query to find Nth highest salary of a employee.

select sal from emp t where &n = (select count(sal) from (select distinct sal         


        
9条回答
  •  自闭症患者
    2020-12-15 02:42

      SELECT Max(Salary) as Salary
        FROM employee
        where  Salary Not in 
        (SELECT TOP N Salary FROM employee ORDER BY Salary DESC)
      where N is defined by you.
    

    So let's say you have the following salaries in the table employee: Here employeeID and Salary are the columns of employee table.

    EmployeeID Salary

     101  25,000
     154  89,000
     987  42,000
     450  12,000
     954  50,000
    

    If we want to see the fourth-highest salary

    Salary

    25,000
    

    Query return fourth highest salary.

提交回复
热议问题