Nth max salary in Oracle

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

    select min(sal) from (select distinct(sal) from emp  order by sal desc) where rownum <=&n;
    

    Inner query select distinct(sal) from emp order by sal desc will give the below output as given below.

    SAL 5000 3000 2975 2850 2450 1600 1500 1300 1250 1100 950 800

    without distinct in the above query select sal from emp order by sal desc output as given below.

    SAL 5000 3000 3000 2975 2850 2450 1600 1500 1300 1250 1250 1100 950 800

    outer query will give the 'N'th max sal (E.g) I have tried here for 4th Max sal and out put as given below.

    MIN(SAL) 2850

提交回复
热议问题