前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 0184 - Department Highest Salary

LeetCode 0184 - Department Highest Salary

作者头像
Reck Zhang
发布2021-08-11 14:55:29
3320
发布2021-08-11 14:55:29
举报
文章被收录于专栏:Reck ZhangReck Zhang

Department Highest Salary

Desicription

The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id.

代码语言:javascript
复制
+----+-------+--------+--------------+
| Id | Name  | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1  | Joe   | 70000  | 1            |
| 2  | Henry | 80000  | 2            |
| 3  | Sam   | 60000  | 2            |
| 4  | Max   | 90000  | 1            |
+----+-------+--------+--------------+

The Department table holds all departments of the company.

代码语言:javascript
复制
+----+----------+
| Id | Name     |
+----+----------+
| 1  | IT       |
| 2  | Sales    |
+----+----------+

Write a SQL query to find employees who have the highest salary in each of the departments. For the above tables, Max has the highest salary in the IT department and Henry has the highest salary in the Sales department.

代码语言:javascript
复制
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT         | Max      | 90000  |
| Sales      | Henry    | 80000  |
+------------+----------+--------+

Solution

代码语言:javascript
复制
SELECT 
    D.Name AS Department,
    E.Name AS Employee,
    E.Salary AS Salary
FROM
    Department D,
    Employee E,
    (SELECT DepartmentId AS Id, max(Salary) AS Salary FROM Employee GROUP BY DepartmentId) T
WHERE
    E.DepartmentId = D.Id AND
    E.DepartmentId = T.Id AND
    E.Salary = T.Salary
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-06-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Department Highest Salary
    • Desicription
      • Solution
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档