Top N Salary of employee in a table
Lets there is
[Employee]
table having two columns EmpName
, and Salary
. Something like :
EmpName | Salary |
---|---|
Mark | 30 |
Jacob | 70 |
Larry | 50 |
John | 150 |
First Method
select distinct salary from Employee e1
where N-1 = (select count(distinct salary) from Employee e2 where e1.salary <= e2.salary);
To understand above query lets break query into subpart:
- select count(distinct salary) from Employee e2 where 30<= e2.salary;
- select count(distinct salary) from Employee e2 where 70<= e2.salary;
- select count(distinct salary) from Employee e2 where 50<= e2.salary;
- select count(distinct salary) from Employee e2 where 150<= e2.salary;
Note: to get third highest salary, put N=4 in above query and for highest salary in entire table put N=2 and so on.
Second Method
select Top 1 EmpName ,Salary from Employee Order By Salary DESC;
This is very simple query to get highest salary of employee.
Third Method
select MAX(Salary) as HighestSalary from Employee Where Salary NOT IN (
Select Top 2 Salary From Employee Order By Salary DESC);
To get top salary getter first of all we'll get top 2 salary from table and after that we'll get top 1 from that record set.
Tutorials on DotNet Palace on SQL Server
SQL Server Tutorials
DotNet Architecture
C# Tutorials
ASP.Net MVC Tutorials