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

+----+-------+--------+--------------+
| Id | Name | S
alary | 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.

+----+----------+
| 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.

+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| Sales | Henry | 80000 |
+------------+----------+--------+

考察怎么做groupby和join吧

select D.name as Department, E.Name as Employee, E.Salary from Employee as E
INNER JOIN
(select DepartmentId as did, max(Salary) as sal from Employee group by DepartmentId) T
ON E.Salary = T.sal and E.DepartmentId = T.did
INNER JOIN
Department AS D
ON D.Id = T.did

LeetCode DB: Department Highest Salary的更多相关文章

  1. [LeetCode#184]Department Highest Salary

    The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...

  2. leetcode Database3(Nth Highest Salary<—>Consecutive Numbers<—>Department Highest Salary)

    一.Nth Highest Salary Write a SQL query to get the nth highest salary from the Employee table. +----+ ...

  3. [LeetCode] Department Highest Salary -- 数据库知识(mysql)

    184. Department Highest Salary The Employee table holds all employees. Every employee has an Id, a s ...

  4. [LeetCode] Department Highest Salary 系里最高薪水

    The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...

  5. LeetCode——Department Highest Salary(花式使用IN以及GROUP BY)

    The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...

  6. [SQL]LeetCode184. 部门工资最高的员工 | Department Highest Salary

    The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...

  7. LeetCode 176 Second Highest Salary mysql,select 嵌套 难度:1

    https://leetcode.com/problems/second-highest-salary/ Write a SQL query to get the second highest sal ...

  8. 【SQL】184. Department Highest Salary

    The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...

  9. LeetCode 177. Nth Highest Salary

    https://leetcode.com/problems/nth-highest-salary/description/ Write a SQL query to get the nth highe ...

随机推荐

  1. centos7----pstree

    centos 默认没有pstree 安装 yum -y install psmisc

  2. Python-WSGI协议如何实现?

    简述浏览器通过WSGI 请求动态资源的过程? 发送 http 请求动态资源给 web 服务器 web 服务器收到请求后通过 WSGI 调用一个属性给应用程序框架 应用程序框架通过引用 WSGI 调用 ...

  3. CSS3标签显示模式

    HTML标签一般分为块标签和行内标签两种类型,它们也称块元素和行内元素.具体如下: 块级元素(block-level) 每个块元素通常都会独自占据一整行或多整行,可以对其设置宽度.高度.对齐等属性,常 ...

  4. 不一样的日期、时间转换(moment.js)

    无意中遇到了一种很奇怪的日期格式,从接口中返回的日期是这样的,如 2018-02-06T11:59:22+08:00 .然而这却不是我们想要的,我们要的是这种,YYYY-MM-DD HH:mm:ss. ...

  5. elasticsearch+logstash+redis+kibana 实时分析nginx日志

    1. 部署环境 2. 架构拓扑 3. nginx安装 安装在192.168.176.128服务器上 这里安装就简单粗暴了直接yum安装nginx [root@manager ~]# yum -y in ...

  6. 3DMax——基础

    1.首次打开3DMAX设置单位: 自定义→单位设置→①系统单位设置→1单位=1.0毫米:②公制→毫米 注:室内单位为毫米,室外单位为米 2.从CAD导出可以导入到3DMAX的文件: 选中要导出的部分→ ...

  7. opencv2函数学习之flip:实现图像翻转

    在opencv2中,flip函数用来进行图片的翻转,包括水平翻转,垂直翻转,以及水平垂直翻转. void flip(const Mat& src, Mat& dst, int flip ...

  8. python应用

    GUI(图形用户界面) python是可以创建GUI的,使用第三方库一般是Tk.wxWidgets.Qt.GTK. 而python自带的是支持Tk的Tkinter,我们这里就来用Tkinter来实现G ...

  9. WTF小程序之<web-view>

    叨叨两句 昨天爬了一下午坑才出来的我向大家问好

  10. GO入门——4. 数组、切片与map

    1. 数组 定义数组的格式:var [n],n>=0 数组长度也是类型的一部分,因此具有不同长度的数组为不同类型 注意区分指向数组的指针和指针数组 //数组的指针 a := [2]int{1, ...