I one of the interview I have got a question like "how can we get nth highest and lowest salary on an employee ?".
In this post I am going to expose, how can we achieve this in SQL Server.
Suppose we have employee name and salary as shown in below figure.
Query to get nth(3rd) Highest Salary
Select TOP 1 Salary as '3 rd Highest Salary'
from (SELECT DISTINCT TOP 3 Salary from tbl_Employee ORDER BY Salary DESC)
a ORDER BY Salary ASC
Query to get nth(3rd) Lowest Salary
Select TOP 1 Salary as '3 rd Lowest Salary'
from (SELECT DISTINCT TOP 3 Salary from tbl_Employee ORDER BY Salary ASC)
a ORDER BY Salary DESC
In this post, we learn on how can we get nth highest and lowest salary of an employee.
In this post I am going to expose, how can we achieve this in SQL Server.
Suppose we have employee name and salary as shown in below figure.
Query to get nth(3rd) Highest Salary
Select TOP 1 Salary as '3 rd Highest Salary'
from (SELECT DISTINCT TOP 3 Salary from tbl_Employee ORDER BY Salary DESC)
a ORDER BY Salary ASC
Query to get nth(3rd) Lowest Salary
Select TOP 1 Salary as '3 rd Lowest Salary'
from (SELECT DISTINCT TOP 3 Salary from tbl_Employee ORDER BY Salary ASC)
a ORDER BY Salary DESC
In this post, we learn on how can we get nth highest and lowest salary of an employee.
No comments:
Post a Comment