In how many ways can you calculate the employee having maximum salary?
Below are few methods by which maximum salary can be calculated:
Way 1: get the max in the inner query and do a inner join
select EMPLOYID,EMPLOYNAME,EMPLOYSALARY from
( select MAX(EMPLOYSALARY) MAX_EMPLOYSALARY
from EMPLOY ) a inner join EMPLOY on MAX_EMPLOYSALARY=EMPLOYSALARY
Way 2: apply rank and get the rank=1
select EMPLOYID,EMPLOYNAME,EMPLOYSALARY from
( select EMPLOYID,EMPLOYNAME,EMPLOYSALARY,RANK() OVER(ORDER BY EMPLOYSALARY DESC)r
from EMPLOY )a where r=1
Way 3: get the top 1 and order by EMPLOYSALARY
select top 1 EMPLOYID,EMPLOYNAME,EMPLOYSALARY
from EMPLOY
order by EMPLOYSALARY desc
Way 4: use in where condition
select EMPLOYID,EMPLOYNAME,EMPLOYSALARY from EMPLOY where EMPLOYSALARY = ( select max(EMPLOYSALARY) from EMPLOY )
Way 5: using a CTE(common table expression)
with cte as
( select EMPLOYEE,EMPLOYNAME,EMPLOYEE SALARY,RANK() OVER(ORDER BY EMPLOYSALARY DESC)r
from EMPLOY )
select EMPLOYEE,EMPLOYNAME,EMPLOYEE SALARY from cte where r=1
We can use this to find any Nth level of rank as well e.g. fo 8th rank:
with cte as
( select EMPLOYEE,EMPLOYNAME,EMPLOYEE SALARY,RANK() OVER(ORDER BY EMPLOYSALARY DESC)r
from EMPLOY )
select EMPLOYEEID,EMPLOYEENAME,EMPLOYEE SALARY from cte where r=8