Find Nth max/min Salary in SQL server

  1. SELECT * FROM temp1 t1 WHERE n - 1 = ( SELECT COUNT(DISTICNT ( sal )) FROM temp1 t2 WHERE t2.sal < t1.sal )
  2. SELECT TOP 1 * FROM ( SELECT DISTINT TOP n sal FROM temp1 ORDER BY sal DESC) AS t ORDER BY t.sal ASC
  3. In sql server 2005
    WITH MyCTE AS(
     SELECT t_id,
      sal,
      DENSE_RANK() OVER(ORDER BY sal DESC) as rank1
     FROM temp1
     )
    SELECT TOP 1 t_id, sal, rank1 FROM MyCTE WHERE rank1 = n

Any updates/comments are appreciated.

Comments

Popular posts from this blog

SharePoint 2019 New Features

Comparing Windows Kerberos and NTLM Authentication Protocols

Move the list items from one list to another list using PowerShell - SharePoint 2010