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

What is the recommended interval at which to run statspack snapshots, and why?

2430


How to create a new oracle data file?

550


Where is the export dump file located?

567


Why do you use stored procedures and state some of its disadvantages?

563


Using the relations and the rules set out in the notes under each relation, write statements to create the two sequence generators specified in the notes.

1642






while i take backup using ibm tsm the following errors occurred: rman-03009 ora-19513 ora-27206 ora-19502 ora-27030 ora-ora19511

3865


What is a cursor in oracle?

644


Why we use bulk collect in oracle?

532


What is a connect identifier?

536


How to add a new column to an existing table in oracle?

584


How can I create database in oracle?

565


How to delete a column in an existing table?

589


How do you find current date and time in oracle?

586


What is an Oracle index?

1173


Write a trigger example in oracle?

570