Second Highest Salary

后端 未结 10 1590
无人共我
无人共我 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 07:05

    select case 
    when cnt>1 then SecondHighestSalary
    else null end as SecondHighestSalary
    from 
    (select top 1 Salary as SecondHighestSalary,
    (select count(distinct Salary)  from Employee) as cnt 
    from (
    select distinct top 2 Salary 
    from Employee
    order by Salary desc ) as sal
    order by SecondHighestSalary asc) as b
    

提交回复
热议问题