I'm trying to write an SQL statement to return the second largest applicationid anyone got any idea of how to do this?
This was a question ask of me so I do not have any code to start, I was thinking that SQL has a built in method to do this.
I'm trying to write an SQL statement to return the second largest applicationid anyone got any idea of how to do this?
This was a question ask of me so I do not have any code to start, I was thinking that SQL has a built in method to do this.
If you are using SQL Server 2012 and ApplicationId
is unique in the table being queried:
select ApplicationId
from Applications
order by ApplicationId desc
offset 1 fetch first 1 row only;
You could use the rank
window function:
SELECT applicationid
FROM (SELECT applicationid,
DENSE_RANK() OVER (ORDER BY applicationid DESC) AS rk
FROM mytable)
WHERE rk = 2
select ApplicationId
from Applications a1 where 3=(select count(ApplicationId)
from Applications a2 where a2.ApplicationId>a1.ApplicationId)