Second Highest Salary in SQL Server

create table tbemp(empno int, ename varchar(50), eadd varchar(50), esal int, edno int)
GO
insert tbemp values(1,'Amit','sample address',12000,10)
insert tbemp values(2,'Raj','sample address',14000,20)
insert tbemp values(3,'John','sample address',18000,30)
insert tbemp values(4,'Rajni','sample address',20000,10)
insert tbemp values(5,'Suraj','sample address',18000,20)
insert tbemp values(6,'Rohit','sample address',22000,10)
insert tbemp values(7,'Bharat','sample address',12000,30) 

Second highest salary

First Query

select sr,empno,esal,ename from
(select empno,esal,ename, ROW_NUMBER() over(order by esal desc) as sr from tbemp) as p
where p.sr=2 order by esal desc 

Second Query to get second highest salary

select top 1 empno, esal,ename from
(select top 2 empno, esal,ename from tbemp order by esal desc) as p
order by esal asc

Ebook Download
View all
Learn
View all