Nth max salary in Oracle

前端 未结 26 1571
名媛妹妹
名媛妹妹 2020-11-30 07:02

To find out the Nth max sal in oracle i\'m using below query

SELECT DISTINCE sal 
FROM emp a 
WHERE (
       SELECT COUNT(DISTINCE sal) 
       FROM emp b 
          


        
相关标签:
26条回答
  • 2020-11-30 07:31
    select * from (select rownum as rownumber,emp1.* from (select * from emp order by sal desc) emp1) where rownumber = 3;
    
    0 讨论(0)
  • 2020-11-30 07:32

    We could write as below mentioned also.

    select min(sal) from (select sal from emp where rownum=<&n order by sal desc);
    
    0 讨论(0)
  • 2020-11-30 07:33
    SELECT *
        FROM (
                    SELECT empno,
                           deptno, sal,
                           dense_rank( ) over ( order by sal desc) NRANK
                    FROM emp
                )
        WHERE NRANK = 4
    
    0 讨论(0)
  • 2020-11-30 07:34

    try this

    select *
      from
      (
        select
            sal
              ,dense_rank() over (order by sal desc) ranking
        from   table
      )
      where ranking = 4 -- Replace 4 with any value of N
    
    0 讨论(0)
  • 2020-11-30 07:35
    select * FROM (
    select EmployeeID, Salary
    , dense_rank() over (order by Salary DESC) ranking
    from Employee
    )
    WHERE ranking = N;
    

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

    0 讨论(0)
  • 2020-11-30 07:36
     SELECT sal
        FROM (
                    SELECT empno,
                                 deptno, sal,
                                  dense_rank( ) over ( partition by deptno order by sal desc) NRANK
                    FROM emp
                )
        WHERE NRANK = 4
    
    0 讨论(0)
提交回复
热议问题