Home » SQL & PL/SQL » SQL & PL/SQL » Top 2nd salary
Top 2nd salary [message #1682] Wed, 22 May 2002 03:13 Go to next message
shankar
Messages: 29
Registered: January 2002
Junior Member
How to find the top 2 and 3 salary in the emptable
Re: Top 2nd salary [message #1696 is a reply to message #1682] Thu, 23 May 2002 02:08 Go to previous message
Maaher
Messages: 7065
Registered: December 2001
Senior Member
Try the following:
You select the top-3 salaries minus the top-1 salary.
Beware of double salaries!

SELECT empno
, sal
from (select empno, sal
from emp
order by sal desc
)
where rownum < 4
minus
SELECT empno
, sal
from (select empno, sal
from emp
order by sal desc
)
where rownum < 2
/
======
to include duplicate salaries, try the following (it's from the top of my head, so it is possible that there's a more simple solution):

select e.empno
, e.sal
from emp e
, ( SELECT sal
FROM ( SELECT distinct(e.sal)
FROM emp e
ORDER BY 1 desc
)
WHERE rownum < 4
) f
where e.sal = f.sal
minus
select e.empno
, e.sal
from emp e
, ( SELECT sal
FROM ( SELECT distinct(e.sal)
FROM emp e
ORDER BY 1 desc
)
WHERE rownum < 2
) f
where e.sal = f.sal
order by 2 desc
/
=========

Regards,
MHE
Previous Topic: DBMS_SQL.BIND_VARIABLE example
Next Topic: How to generate RANDOM Number in Oracle
Goto Forum:
  


Current Time: Thu May 09 21:36:51 CDT 2024