Nth max salary in Oracle

前端 未结 26 1575
名媛妹妹
名媛妹妹 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:55

    There are three methods are there...

    SELECT salary,first_name,rnk 
    FROM (SELECT salary,first_name,rank() over (order by salary desc nulls last) as                                                                  rnk from emp) where rnk=3;
    
    
    SELECT salary,first_name,rnk 
    FROM (SELECT salary,first_name,dense_rank() over (order by salary desc nulls last) as                                                                  rnk from emp) where rnk=3;
    
    
    select rnk,first_name,salary 
    from (select rownum as rnk ,first_name,salary 
          from (select first_name,salary 
                from emp order by salary desc nulls last)) where rnk=3
    
    0 讨论(0)
  • 2020-11-30 07:57

    This will show the 3rd max salary from table employee. If you want to find out the 5th or 6th (whatever you want) value then just change the where condition like this where rownum<=5" or "where rownum<=6 and so on...

    select min(sal) from(select distinct(sal) from emp  where rownum<=3 order by sal desc);
    
    0 讨论(0)
提交回复
热议问题