Finding the highest salary in SQL Server

Table: tbemployee
Query to find the first highest salary:
Here we can use any one of the following query...

SELECT TOP 1  empsalary
FROM ( SELECT DISTINCT TOP 1 empsalary   FROM tbemployee   ORDER BY empsalary DESC) a
ORDER BY empsalary


select empid, empname ,empsalary from (select empid,empname,empsalary, ROW_NUMBER() over (Order by empsalary desc) as salary_order from tbemployee
) DT
where DT.salary_order=1

Ouput would be like this:




But if the salary is similar i.e. Salary with a tie or we can say that salary is same of two employees then we can use the below mention query:

select empid, empname ,empsalary from (select empid,empname,empsalary, dense_rank() over (Order by empsalary desc) as salary_order from tbemployee
) DT     where DT.salary_order=1




click here for : Third Highest Salary



0 comments:

Post a Comment