How to find second maximum value from a table?

Showing Answers 1 - 17 of 17 Answers

suresh

  • Jun 27th, 2006
 

select max(field1) from tname1 where field1=(select max(field1) from tname1 where field1<(select max(field1) from tname1);

Field1- Salary field

Tname= Table name.

  Was this answer useful?  Yes

darshil

  • Jun 28th, 2006
 

simpler way would be:select min(column) from (select column from table order by column desc) where rownum <= 2cheers....darshil

  Was this answer useful?  Yes

Santosh

  • Jul 1st, 2006
 

Very Simple.........

select max(field1) from table_name where field1 < (select max(field1) from table_name)

  Was this answer useful?  Yes

Palak

  • Jul 3rd, 2006
 

select max(column name) from employee where column name NOT IN (select max(column name) from employee)

  Was this answer useful?  Yes

irshad

  • Jul 6th, 2006
 

SELECT MAX(Column1) FROM Tables WHERE Column1 < (SELECT MAX(Column1) FROM Table1)

  Was this answer useful?  Yes

pankaj kaushik

  • Jul 21st, 2006
 

Select Top 1 Field1 from TableName Where Field1 <=(Select Max(Field1) from TableName) Order by Field1

  Was this answer useful?  Yes

shriramesh

  • Jul 22nd, 2006
 

SELECT max(fieldname)-1 as v FROM tablename

Here : v = variablename

  Was this answer useful?  Yes

Guest

  • Jul 22nd, 2006
 

SELECT max(fieldname) from tablename where fieldname <= select fieldname from tablename where fieldname<max(fieldname)

  Was this answer useful?  Yes

Sachin

  • Aug 3rd, 2006
 

This is the most simplest way for finding any highest number (1,2,3,...n)

select a.* from emp a
where n=(select count(distinct b.empsal) from emp b where a.empsal<=b.empsal)

--where n =2 for the second  highest.

  Was this answer useful?  Yes

yakhubpasha

  • Aug 12th, 2006
 

      select min(sal) from emp where sal in

        ( select top(2) from emp

         order by sal);

         

  Was this answer useful?  Yes

Vijay Soni

  • Aug 16th, 2006
 

select max(t1.column_name) from table t1 where n-1 < (select count(distinct(t2.column_name)) from table t2 where t1.column_name > t2.column_name)

note: t1,t2 are the alias of same table this is inner join and (n-1) means put n=(the valuue u want to findout in this case put n=2)

  Was this answer useful?  Yes

chanti4u123

  • Sep 21st, 2006
 

suppose if the sal max is 5000 and the second max sal is 3000 then

select * from emp where sal=(select max(sal) from emp where sal<(select max(sal) from emp));

  Was this answer useful?  Yes

In Teradata, below query can be used for 2nd max even if duplicates are present.
sel b.* from 
(
sel a.*,rank() over (order by a.salary desc) as rnk from
(sel salary  from samples.emp
group by salary ) as a
) b
where b.rnk=2

  Was this answer useful?  Yes

Akila

  • Oct 11th, 2018
 

SELECT *
FROM
(SELECT personid ,row_number() over (order by PersonID desc) as row_num FROM
(SELECT DISTINCT personid FROM persons))
WHERE row_num = 2

  Was this answer useful?  Yes

Pendo

  • Jul 17th, 2021
 

WITH Xlist as (
SELECT salary ,rank() over( order by salary desc ) as rnk
FROM salary)
SELECT salary from Xlist where rnk =2;

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions