How to find top three highest salary in emp table

2020-02-24 04:09发布

How to find top three highest salary in emp table in oracle?

17条回答
来,给爷笑一个
2楼-- · 2020-02-24 05:09

Something like the following should do it.

SELECT  Name, Salary
FROM 
    (
    SELECT  Name, Salary
    FROM         emp 
    ORDER BY Salary desc
    )
WHERE rownum <= 3
ORDER BY Salary ;
查看更多
Explosion°爆炸
3楼-- · 2020-02-24 05:09

You could use DBMS_STAT_FUNCS.Summary:

SET SERVEROUTPUT ON;
DECLARE
    s DBMS_STAT_FUNCS.SummaryType;
BEGIN
    DBMS_STAT_FUNCS.SUMMARY('HR', 'EMPLOYEES', 'SALARY',3, s);
    DBMS_OUTPUT.put_line('Top 3: ' || s.top_5_values(1) || '-' 
                         || s.top_5_values(2) || '-' || s.top_5_values(3));
END;
/

Output:

Top 3: 24000-17000-17000
查看更多
We Are One
4楼-- · 2020-02-24 05:12
SELECT * FROM
     (
      SELECT  ename, sal,
      DENSE_RANK() OVER (ORDER BY SAL DESC) EMPRANK
      FROM emp 
     )
    emp1 WHERE emprank <=5
查看更多
Luminary・发光体
5楼-- · 2020-02-24 05:12
select top 3 * from emp  order by sal desc
查看更多
Emotional °昔
6楼-- · 2020-02-24 05:14
SELECT DISTINCT(salary) FROM emp order by salary asc limit 0 ,3

Above query gives three highest salary with DISTINCT.

查看更多
登录 后发表回答