题目描述
The Employee
table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id.
1 | +----+-------+--------+--------------+ |
The Department
table holds all departments of the company.
1 | +----+----------+ |
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).
1 | +------------+----------+--------+ |
Explanation:
Max and Jim both have the highest salary in the IT department and Henry has the highest salary in the Sales department.
首先需要把两张表根据departmentid join起来,接着需要在employee表中找到每个department中salary的最高值,并且记录其对应的departmentid,最后在join起来的最终结果中寻找每个(departmentid, 最高的salary)所对应的组。
代码实现
1 | select department.name as Department, employee.name as Employee, Salary |