Second Highest Salary

后端 未结 10 1488
无人共我
无人共我 2020-12-20 06:14

Write a SQL query to get the second highest salary from the Employee table.

    | Id | Salary |
    | 1  | 100    |
    | 2  | 200    |
    | 3  | 300    |
<         


        
10条回答
  •  醉梦人生
    2020-12-20 06:58

    I would use DENSE_RANK() & do LEFT JOIN with employee table :

    SELECT t.Seq, e.*
    FROM ( VALUES (2) 
         ) t (Seq) LEFT JOIN
         (SELECT e.*,
                 DENSE_RANK() OVER (ORDER BY Salary DESC) AS Num
          FROM Employee e
         ) e 
         ON e.Num = t.Seq;
    

提交回复
热议问题