1 October 2013

Get nth highest and lowest salary of an employee table

Create Employee table
create table tbl_employee(Id int identity(100,1) primary key,Name varchar(200),Sal decimal,address varchar(200))
insert data
insert into tbl_employee(Name,Sal,Address)values('adi',20000,'bangalore')
insert into  tbl_employee values('pavan',30000,'bangalore')
insert into tbl_employee values('Ekanath',40000,'bangalore')
insert into tbl_employee values('ravi',13000,'bangalore')
insert into tbl_employee values('sinu',50000,'bangalore')
 select  data from table
select * from tbl_employee
Output


1st Highest Salary
Select TOP 1 Sal as '1st Highest Salary'
from (SELECT DISTINCT TOP 1 Sal from tbl_employee ORDER BY Sal DESC)
a ORDER BY Sal ASC
Output

1st Lowest  Salary
Select TOP 1 Sal as '1st Lowest Salary'
from (SELECT DISTINCT TOP 1 Sal from tbl_employee ORDER BY Sal ASC)
a ORDER BY Sal DESC
Output

3rd Highest Salary (or) Nth Highest Salary
Select TOP 1 Sal as '3rd Highest Salary'
from (SELECT DISTINCT TOP 3 Sal from tbl_employee ORDER BY Sal DESC)
a ORDER BY Sal ASC
Output

3rd Lowest  Salary (or) Nth Lowest  Salary
Select TOP 1 Sal as '3rd Lowest Salary'
from (SELECT DISTINCT TOP 3 Sal from tbl_employee ORDER BY Sal ASC)
a ORDER BY Sal DESC
Output

No comments:

Post a Comment