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 difference between post-database commit and post-form commit?

517


What privilege is needed for a user to create tables in oracle?

550


How to see free space of each tablespace?

596


types of indexes and the rationale behind choosing a particular index for a situation.

1986


Explain user account with reference to oracle.

538






What is concurrency in oracle?

547


How to use existing values in update statements using oracle?

545


Which is better Oracle or MS SQL? Why?

3978


What is parameterized cursor in oracle?

578


What are a query and state the different types of queries and their uses?

512


How many data types are supported?

622


How we can able to import our own template (users designed MS-Word templates) which has many tabular columns; need to pass some values generate by Oracle-reports9i? Actually need to import more than 400 MS-Word templates into Oracle Reports-9i to minimize layout design in Reports.

1615


how do u setup a replication site?

1502


Please explain drop constraint oracle?

629


What is the difference between the Oracle ODBC driver and a Microsoft ODBC (Open Database Connectivity) Driver?

620