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

后端 未结 18 1271
孤城傲影
孤城傲影 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 17:50
    select * from emp where sal=(select max(sal) from emp where sal<(select max(sal) from emp))
    

    so in our emp table(default provided by oracle) here is the output

    EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


      7698 BLAKE      MANAGER         7839 01-MAY-81       3000            30
      7788 SCOTT      ANALYST         7566 19-APR-87       3000            20
      7902 FORD       ANALYST         7566 03-DEC-81       3000            20
    

    or just you want 2nd maximum salary to be displayed

    select max(sal) from emp where sal<(select max(sal) from emp)
    

    MAX(SAL)

      3000
    
    0 讨论(0)
  • 2020-12-30 17:53

    select Max(Salary) as SecondHighestSalary from Employee where Salary not in (select max(Salary) from Employee)

    0 讨论(0)
  • 2020-12-30 17:54

    SELECT * FROM EMP WHERE SAL=(SELECT MAX(SAL) FROM EMP WHERE SAL<(SELECT MAX(SAL) FROM EMP));

    (OR) SELECT ENAME ,SAL FROM EMP ORDER BY SAL DESC;

    (OR) SELECT * FROM(SELECT ENAME,SAL ,DENSE_RANK() OVER(PARTITION BY DEPTNO ORDER BY SAL DESC) R FROM EMP) WHERE R=2;

    0 讨论(0)
  • 2020-12-30 17:55

    RANK and DENSE_RANK have already been suggested - depending on your requirements, you might also consider ROW_NUMBER():

    select * from (
      select e.*, row_number() over (order by sal desc) rn from emp e
    )
    where rn = 2;
    

    The difference between RANK(), DENSE_RANK() and ROW_NUMBER() boils down to:

    • ROW_NUMBER() always generates a unique ranking; if the ORDER BY clause cannot distinguish between two rows, it will still give them different rankings (randomly)
    • RANK() and DENSE_RANK() will give the same ranking to rows that cannot be distinguished by the ORDER BY clause
    • DENSE_RANK() will always generate a contiguous sequence of ranks (1,2,3,...), whereas RANK() will leave gaps after two or more rows with the same rank (think "Olympic Games": if two athletes win the gold medal, there is no second place, only third)

    So, if you only want one employee (even if there are several with the 2nd highest salary), I'd recommend ROW_NUMBER().

    0 讨论(0)
  • 2020-12-30 17:56
    select * FROM (
    select EmployeeID, Salary
    , dense_rank() over (order by Salary DESC) ranking
    from Employee
    )
    WHERE ranking = 2;
    

    dense_rank() is used for the salary has to be same.So it give the proper output instead of using rank().

    0 讨论(0)
  • 2020-12-30 17:56

    I would suggest following two ways to implement this in Oracle.

    1. Using Sub-query:
    select distinct SALARY   
    from EMPLOYEE e1  
    where 1=(select count(DISTINCT e2.SALARY) from EMPLOYEE e2 where         
      e2.SALARY>e1.SALARY);
    

    This is very simple query to get required output. However, this query is quite slow as each salary in inner query is compared with all distinct salaries.

    1. Using DENSE_RANK():
    select distinct SALARY   
    from
      (
        select e1.*, DENSE_RANK () OVER (order by SALARY desc) as RN 
        from EMPLOYEE e
      ) E
     where E.RN=2;
    

    This is very efficient query. It works well with DENSE_RANK() which assigns consecutive ranks unlike RANK() which assigns next rank depending on row number which is like olympic medaling.

    Difference between RANK() and DENSE_RANK(): https://oracle-base.com/articles/misc/rank-dense-rank-first-last-analytic-functions

    0 讨论(0)
提交回复
热议问题