且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

​LeetCode刷题实战184:部门工资最高的员工

更新时间:2022-03-19 20:01:34

今天和大家聊的问题叫做 部门工资最高的员工  ,我们先来看题面:https://leetcode-cn.com/problems/department-highest-salary/

Write a SQL query to find employees who have the highest salary in each of the departments. For the above tables, your SQL query should return the following rows (order of rows does not matter).

题意


编写一个 SQL 查询,找出每个部门工资最高的员工。对于上述表,您的 SQL 查询应返回以下行(行的顺序无关紧要)。

​LeetCode刷题实战184:部门工资最高的员工

解题

此题难度中等,可以使用 JOIN 和 IN 语句,查询语句如下:

SELECT
    Department.name AS 'Department',
    Employee.name AS 'Employee',
    Salary
FROM
    Employee
        JOIN
    Department ON Employee.DepartmentId = Department.Id
WHERE
    (Employee.DepartmentId , Salary) IN
    ( SELECT
            DepartmentId, MAX(Salary)
        FROM
            Employee
        GROUP BY DepartmentId
  )
;

好了,今天的文章就到这里,如果觉得有所收获,请顺手点个在看或者转发吧,你们的支持是我最大的动力 。