I have a table EmpDetails:
EmpDetails
DeptID EmpName Salary Engg Sam 1000 Engg Smith 2000 HR Denis 1500 HR
WITH cteRowNum AS ( SELECT DeptID, EmpName, Salary, ROW_NUMBER() OVER(PARTITION BY DeptID ORDER BY Salary DESC) AS RowNum FROM EmpDetails ) SELECT DeptID, EmpName, Salary,Rownum FROM cteRowNum WHERE RowNum in(1,2);