How to get second largest or third largest entry from a table

前端 未结 12 2507
说谎
说谎 2020-12-01 08:42

Can anyone please tell me how to find out the N th largest entry from a table in Oracle?

Like for largest we can use MAX(column_name) i

12条回答
  •  被撕碎了的回忆
    2020-12-01 09:18

    SELECT DISTINCT (a.sal) FROM EMP A WHERE &N = (
      SELECT COUNT (DISTINCT (b.sal)) FROM EMP B WHERE a.sal<=b.sal
    );
    

    Replace &N with you desired number. For example, 2 will give you the second largest salary.

    If you are using PL/SQL, just execute the statement. It will prompt for N.

提交回复
热议问题