Second Highest Salary

Write an SQL query to report the second highest salary from the Employee table. If there is no second highest salary, the query should report null.

table name: employee


Solution:

declare @salary int;
select top 1 @salary = salary from employee1 where salary not in (select max(salary) from employee1) order by Salary desc
select @salary as SecondHighestSalary 

Output:


SQL Script:

Create table Employee1 (id int, salary int)
insert into Employee1 (id, salary) values ('1', '100')
insert into Employee1 (id, salary) values ('2', '200')
insert into Employee1 (id, salary) values ('3', '300')


Comments (0)