Tuesday, 22 April 2014

Find out 3rd highest Basic_sal

  CREATE TABLE Employee([Eid] [int] NULL,[Ename] [nvarchar](255) NULL,[Basic_Sal] [float] NULL)

  insert into Employee values(1,'Neeraj',45000)
insert into Employee values(2,'Ankit',5000)
insert into Employee values(3,'Akshay',6000)
insert into Employee values(4,'Ramesh',7600)
insert into Employee values(5,'Vikas',4000)
insert into Employee values(7,'Neha',8500)
insert into Employee values(8,'Shivika',4500)
insert into Employee values(9,'Tarun',9500)

select Ename,Basic_sal
from(select Ename,Basic_Sal,ROW_NUMBER() over (order by Basic_Sal desc) as rowid from Employee)A
where rowid=3
Based on the rowid we can find out the salary in any order...


select Ename,Basic_sal, [rank]
from(select Ename,Basic_Sal,DENSE_RANK() over (order by Basic_Sal desc) as [rank] from Employee)A
where [rank]=3


No comments:

Post a Comment