The challenge is to find the employees with the second highest salary in each department. However, it is a little more complicated because if two employees have the same salary, you need to list both of them.
Solution to TSQL Beginners Challenge 1
SELECT EmployeeID, EmployeeName, Department, Salary FROM ( SELECT EmployeeID, EmployeeName, Department, Salary, RANK() OVER(PARTITION BY Department ORDER BY Salary DESC) AS RankNo FROM @Employees ) AS FOO WHERE RankNo = 2
Tags: