Query to get max and second max in oracle in one query ?

Answer Posted / mats önnerby

--- Three level query
--- 1. Find and order all distinct salaries
--- 2. Pick the two top salaries
--- 3. Get all information about employees with that salary

select *
from emp
where sal in (
select sal from (
select distinct sal
from emp
order by sal desc)
where rownum <= 2);
---
--- Simple and straight forward but will return
--- several employees in case they have the same salary
---

Is This Answer Correct ?    12 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can group functions be mixed with non-group selection fields?

542


What is the difference between PFILE and SPFILE in Oracle?

586


how may join possible between (requisition with purchase order)

1712


Compare and contrast between sql and sql server and explain its various functions?

549


What is sharded cluster?

534






Assuming that you are an End User How to find that in the payment Batch some of the Invoice was  Missing To pay How to find That??

1290


What is Reduced List of Values?

1716


What is a private synonym?

1094


How many objectname will be created for a single table drop function? Why 'flashback' query giving error "ORA-38312: original name is used by an existing object" while getting the table?

1814


What is Data Dictionary Cache in Oracle?

669


How do I spool in oracle?

552


Does oracle database need java?

535


HI ALL, CAN ANYONE TELL ME THE DIFFERENCES BETWEEN SQL CLUSTURS,MSQL CLUSTERS,ORACLE CLUSTERS.......THANKS IN ADVANCE

1797


How to write a left outer join with the where clause in oracle?

643


What is ordinary table in oracle?

661