Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


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

Answers were Sorted based on User's Feedback



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

Answer / sumit wadhwa

select distinct(a.colname) from table a where &n=(select
count(distinct(b.colname)) from table b where
a.colname<=b.colname);

from this query u can find out Nth MAX in a one query

eg
select distinct(a.sal) from emp a where &n=(select count
(distinct(b.sal)) from emp b where a.sal<=b.sal);

Is This Answer Correct ?    55 Yes 16 No

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

Answer / sumit wadhwa

for max

select a.sal
from emp a
where 1=(select distinct(count(b.sal))
from emp b
where a.sal<=b.sal);

for second max

select a.sal
from emp a
where 2=(select distinct(count(b.sal))
from emp b
where a.sal<=b.sal)

Is This Answer Correct ?    33 Yes 11 No

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

Answer / samir kumar sahoo

SELECT sal FROM(SELECT DISTINCT(sal) FROM employees ORDER
BY sal DESC) WHERE ROWNUM<=2;

Is This Answer Correct ?    23 Yes 2 No

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

Answer / mehul

select max(sal) from emp where sal not in ( select max(sal)
from emp)

Is This Answer Correct ?    29 Yes 12 No

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

Answer / rohit

The below query will provide the max and second max in
oracle in one query

select * from emp e
where 0 = (select count(sal) from emp
where e.sal < sal)
or 1 = (select count(sal) from emp
where e.sal < sal);

Is This Answer Correct ?    17 Yes 8 No

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

Answer / 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

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

Answer / lokanath

select * from emp a where 2 > (select count(distinct sal)
from emp b where b.sal > a.sal)
or else use Top Window functions (or) Analatical functions
select * from
(
select empno,ename,sal,rank() over (order by sal desc)
test from emp
)
where test <= 2

Is This Answer Correct ?    12 Yes 4 No

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

Answer / subodh tiwari

SELECT ENAME,SAL FROM (SELECT ENAME,SAL FROM EMP ORDER BY
SAL DESC) WHERE ROWNUM<=2

Is This Answer Correct ?    15 Yes 9 No

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

Answer / answer

NO NEED TO WATCH ANY OTHER ANSWER .ANSWER 11 IS GOOD.JUST
GO WITH IT

Is This Answer Correct ?    6 Yes 2 No

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

Answer / bhanu yadav

For Nth Max.

select distinct a.sal from emp a where &N= (select count
(distinct b.sal) from emp b where b.sal>=a.sal)

For Nth Min.

select distinct a.sal from emp a where &N= (select count
(distinct b.sal) from emp b where b.sal<=a.sal)

Is This Answer Correct ?    9 Yes 6 No

Post New Answer

More Oracle General Interview Questions

write sql query following source are EmpID, Salary 101 1000 102 2000 103 3000 I want the output format like following empid,sal,composite_sal 101 1000 1000 102 2000 3000 103 3000 6000

4 Answers   TCS,


How to create a single index for multiple columns?

0 Answers  


The table has 3 columns 4 rows. The output is which column has least null values( A COL SHOULD BE THE OUTPUT) Write the query plz. A B C ================ 1 NULL 2 NULL 3 4 NULL 5 6 7 NULL NULL O/P C = 2 4 6

2 Answers   TCS,


how to insert the image in table

2 Answers  


Explain oracle instance.

0 Answers  


What is E-R diagram ?

2 Answers   CGI, Keane India Ltd,


What is dynamic proxy?

0 Answers  


How to Select second Maximum salary in a Table ?

13 Answers   Oracle,


candidate key is subset of super key but not vice-verse explain

0 Answers  


can you explain performance tunning in oracle(sql,PL/SQL)

2 Answers  


What is a Data Dictionary ?

5 Answers  


1) Does oracle have any table which contain all the exceptions and it's code internally?

0 Answers   CTS,


Categories
  • Oracle General Interview Questions Oracle General (1809)
  • Oracle DBA (Database Administration) Interview Questions Oracle DBA (Database Administration) (261)
  • Oracle Call Interface (OCI) Interview Questions Oracle Call Interface (OCI) (10)
  • Oracle Architecture Interview Questions Oracle Architecture (90)
  • Oracle Security Interview Questions Oracle Security (38)
  • Oracle Forms Reports Interview Questions Oracle Forms Reports (510)
  • Oracle Data Integrator (ODI) Interview Questions Oracle Data Integrator (ODI) (120)
  • Oracle ETL Interview Questions Oracle ETL (15)
  • Oracle RAC Interview Questions Oracle RAC (93)
  • Oracle D2K Interview Questions Oracle D2K (72)
  • Oracle AllOther Interview Questions Oracle AllOther (241)