Click here to Skip to main content
16,019,018 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This Query Find Second Highest Id From table

But My question There How (Where Clause Execute)?
LIke WHERE (2)

SQL
SELECT Id
FROM testTabel2 test1
WHERE (2) =
 
(
SELECT COUNT(DISTINCT(test2.Id ))
FROM testTabel2 test2
WHERE test2.Id >test1.Id )
Posted
Updated 6-Jun-13 2:22am
v2

1 solution

Try this:
SQL
SELECT *
FROM (
    SELECT ID, ROW_NUMBER() OVER(ORDER BY ID Desc) AS RowNo
    FROM Table1) AS T
WHERE RowNo = 2


More about: ROW_NUMBER() T-SQL[^]

or
SQL
SELECT TOP(1) ID
FROM (
    SELECT TOP(2) ID
    FROM Table1
    ORDER BY ID DESC) AS T
ORDER BY ID ASC 
 
Share this answer
 
v3
Comments
gvprabu 6-Jun-13 8:28am    
simple but nice.... my +5 :-)
BHAVESH63 7-Aug-13 0:42am    
Hi Maciej Los

I have Return Second Highest Id But I know how this query execute
Maciej Los 6-Jun-13 8:30am    
Thank you ;)
have a look now, i added some extra example ;)
BHAVESH63 7-Aug-13 0:44am    
query debugging but can't stop at

(
SELECT COUNT(DISTINCT(test2.Id ))
FROM testTabel2 test2
WHERE test2.Id >test1.Id )

please tell me how it Execute and what is return by it?
gvprabu 6-Jun-13 8:32am    
very nice example

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