Skip to content
Loading
  • Tapan Patel
    0
    Learning from Puneet's answer, you should go with query 1 (avoiding Rank function in select statement). But just one correction. Try doing distinct on salary becuase some employees may have same salary amount.
     
    1. SELECT TOP 1 CE.Salary   
    2. FROM (  
    3. SELECT DISTINCT TOP 3 Salary FROM #temptable ORDER BY Salary DESC  
    4. ) AS CE  
    5. ORDER BY CE.Salary  
     
    0
  • Purvesh Sangani
    0
    You can get the third highest salary by using limit , by using TOP keyword and sub-query
     
    1. Top Keyword : SELECT TOP 1 salary FROM (SELECT TOP 3 salary FROM Table_Name ORDER BY salary DESC) AS Comp ORDER BY salary ASC
     
    2. limit Keyword : SELECT salary FROM Table_Name ORDER BY salary DESC LIMIT 2, 1
     
    3. by subquery : SELECT salary FROM (SELECT salary FROM Table_Name ORDER BY salary DESC LIMIT 3) AS Comp ORDER BY salary LIMIT 1;
    0
  • 0
    SELECT TOP 1 salary
    FROM
        (SELECT TOP 3 salary
         FROM salary
         ORDER BY salary DESC) AS Comp
    ORDER BY salary ASC
    Here I have a table named salary
     
     

     
    0
  • Jignesh Kumar
    0
    Hi,
    you can achieve in different way, Please refer this link which will help you out,
    https://www.c-sharpcorner.com/article/find-nth-highest-salary-in-sql-server-2017/
    0