You are on page 1of 1

Retrieving Secondhighest salary of the employee

----------------------------------
3 ways we can retrieve

Create Table #temp(ID int ,Salary int)


insert into #temp Values(1,100)
insert into #temp Values(2,200)
insert into #temp Values(3,300)
insert into #temp Values(4,400)

1) By Using Max() function


select MAx(Salary) from #temp where Salary not in (Select MAx(Salary ) from #temp)

2) By using top keyword


Select top 1 * from ( Select top 2 Salary from #temp order by Salary desc
)A order by Salary ASC
3) By using correlated subquery

select Salary from #temp M Where 2=(Select count(*) from #temp S Where
S.Salary>=M.Salary)

You might also like