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

How are extents allocated to a segment?

774


When do I need to use a semicolon vs a slash in oracle sql?

793


How would you best determine why your MVIEW couldnt FAST REFRESH?

1621


How do I escape a reserved word in oracle?

856


Describe the types of sub query?

743


How i can handle exception in large code like 1000 line without distrubing the code or without exception handler sction?

2952


Difference between inner join vs where ?

784


What is java oracle used for?

762


What are the different types of modules in oracle forms?

743


What query tells you how much space a tablespace named test is taking up, and how much space is remaining?

1988


What is execute immediate in oracle?

724


How to pass a cursor variable to a procedure?

784


What is Reduced List of Values?

1889


What happens to the current transaction if the session is killed?

744


What is merge statement used for?

754