Click here to Skip to main content
15,904,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Can any body plz tellme how we can find the second highest number from column in sql server 2005?
No
--
10
20
40
60

the ans should be 40
Posted

SQL
SELECT MAX(SALARY) FROM EMPLOYEE WHERE SALARY < (SELECT MAX(SALARY) FROM EMPLOYEE)
 
Share this answer
 
Comments
AspDotNetDev 30-Aug-10 15:49pm    
Reason for my vote of 5
This improves upon my answer.
SQL
SELECT SALARY FROM Employee t1 WHERE
2=(SELECT COUNT(DISTINCT t2.SALARY) FROM Employee t2 WHERE t2.SALARY >= t1.SALARY)
 
Share this answer
 
I have given this ans in this thread. Check it out

click here
 
Share this answer
 
SQL
SELECT TOP 1
    Column1
FROM Table1
WHERE
    Column1 <> (SELECT MAX(Column1) FROM Table1)
ORDER BY
    Column1 DESC
 
Share this answer
 
Something like:

SELECT TOP 2 column FROM table LIMIT 1, 1


???
 
Share this answer
 
Comments
jayant812 20-Aug-10 1:48am    
There is no LIMIT function in Sql server 2005 so it dosent work
Nyarost 20-Aug-10 3:49am    
NO LIMIT? Wow!

SELECT column, ROW_NUMBER() OVER (ORDER BY column DESC) AS num WHERE num = 2 ?
SQL
SELECT TOP 1 SALARY FROM Employee WHERE SALARY IN (SELECT TOP 2 SALARY FROM Employee ORDER BY ASC)


It can have multiple variants... instead of top, use Max or so.
 
Share this answer
 
Comments
AspDotNetDev 30-Aug-10 13:37pm    
Sandeep, this will not work. You did not order the outer query by the salary descending. In fact, SQL Server should complain that you have an ORDER BY clause on an inner query, which will always be ignored. Also, you say "ORDER BY ASC" (i.e., you don't specify a column to order by). See my answer.
Sandeep Mewara 30-Aug-10 13:39pm    
Sorry to say... but this works. We already have something simialr in place working fine.
Why not have a quick test if you have a SQL environment with you?
Sandeep Mewara 30-Aug-10 13:42pm    
One thing though, might be I missed the Orderby column name. Agreed on this. That was a quick typo to the question! :doh:
AspDotNetDev 30-Aug-10 14:13pm    
It may work for you (once you fix that typo) because there is no ordering on the columns in the outer query and you may get the right one back by accident (as the TOP 1 will choose between them non-deterministically), but it will not work all of the time unless you put an ORDER BY on the outer query.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900