SOLUTION WITH WINDOW FUNCTION ( SQL SERVER )
Intuition\r\n Describe your first thoughts on how to solve this problem. \r\n\r\n# Approach\r\n Describe your approach to solving the problem. \r\n\r\n# Complex
# Intuition\r\n<!-- Describe your first thoughts on how to solve this problem. -->\r\n\r\n# Approach\r\n<!-- Describe your approach to solving the problem. -->\r\n\r\n# Complexity\r\n- Time complexity:\r\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\r\n\r\n- Space complexity:\r\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\r\n\r\n# Code\r\n```\r\n/* Write your T-SQL query statement below */\r\n\r\n\r\nWITH CTE AS (SELECT\r\n    *,\r\n    DENSE_RANK() OVER(PARTITION BY DepartmentID ORDER BY salary DESC) RANKED\r\nFROM\r\n    Employee\r\n)\r\nSELECT\r\n    d.name Department ,\r\n    c.name Employee,\r\n    c.salary\r\nFROM\r\n    CTE c\r\nINNER JOIN\r\n    Department d on c.departmentid = d.id\r\nWHERE\r\n    EXISTS (\r\n\r\n        SELECT 1\r\n        FROM\r\n            CTE C1\r\n        WHERE\r\n            c.id = c1.id\r\n        AND\r\n            c.departmentid = c1.departmentid\r\n        AND\r\n            C1.RANKED = 1\r\n    )\r\n```
['MS SQL Server']