2nd Highest Salary in SQL Server

  1. select MAX(ID) from Employees where ID NOT IN (Select MAX(ID) from Employees)
  2. select MAX(ID) from Employees where ID < (Select MAX(ID) from Employees )
  1. select top 1 ID from (select top 2 ID from Employees order by id descas tblEmployee order by ID ASC
  2.  Using dense_rank to retrieve 2nd Highetst salary if we have duplicate rows:
  3. with CTETable
  4. as
  5. (
  6. select ID, Name, DENSE_RANK() over (order by ID descas Rnk from Employees
  7. )
  8. select Name, ID from CTETable where rnk=2

Leave a Reply

Your email address will not be published. Required fields are marked *