How can I select the record with the 2nd highest salary in database Oracle?

后端 未结 18 1419
孤城傲影
孤城傲影 2020-12-30 17:27

Suppose I have a table employee with id, user_name, salary. How can I select the record with the 2nd highest salary in Oracle?

I googled it, find this solution, is t

18条回答
  •  不知归路
    2020-12-30 18:00

    Syntax it for Sql server

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

    To get 2nd highest salary of employee then we need replace “n” with 2 our query like will be this

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

    3rd highest salary of employee

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

提交回复
热议问题