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

+----+----------+
| 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 |
+------------+----------+--------+ 需求:查询每个部门工资最高的员工

CREATE TABLE Employee(
Id TINYINT UNSIGNED,
Name VARCHAR(20),
Salary DECIMAL(10,2),
DepartmentId TINYINT
)ENGINE=MyISAM CHARSET=utf8;
CREATE TABLE Department(
Id TINYINT UNSIGNED,
Name VARCHAR(20)
)ENGINE=MyISAM CHARSET=utf8;

SELECT b.nm,a.Name,a.salary
FROM employee a INNER JOIN (
SELECT t2.Id,t2.Name nm,MAX(t1.salary) sal
FROM employee t1 INNER JOIN department t2 ON t1.DepartmentId=t2.Id
GROUP BY t1.DepartmentId
)b ON a.salary=b.sal AND a.DepartmentId=b.Id

[LeetCode]-DataBase-Department Highest Salary的更多相关文章

  1. LeetCode DB: Department Highest Salary

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

  2. [LeetCode#184]Department Highest Salary

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

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

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

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

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

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

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

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

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

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

  9. 【SQL】184. Department Highest Salary

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

  10. LeetCode 177. Nth Highest Salary

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

随机推荐

  1. HNUSTOJ-1674 水果消除(搜索或并查集)

    1674: 水果消除 时间限制: 2 Sec  内存限制: 128 MB提交: 335  解决: 164[提交][状态][讨论版] 题目描述 “水果消除”是一款手机游戏,相信大家都玩过或玩过类似的游戏 ...

  2. flask中使用jsonify和json.dumps的区别

    一.实验 python的flask框架为用户提供了直接返回包含json格式数据响应的方法,即jsonify,在开发中会经常用到.如下一段简单的flask后端代码,服务端视图函数根据请求参数返回json ...

  3. [wpf]wpf full screen.

    void window_KeyDown(object sender,KeyEventArgs e) { if(e.Key == Key.F11) { Window.ResizeMode = Resiz ...

  4. 查看ftp创建的用户

    先su然后查看cat /etc/passwd,500以后的是添加的使用者账户.

  5. kafka复习(1)

    一:flume复习 0.JMS(java message service )java消息服务 ----------------------------------------------------- ...

  6. nodejs fs copy本地文件src dst

    1. // fs.writeFileSync(pathNewFile, fs.readFileSync(fileName)); 2.   fs.createReadStream(fileName).p ...

  7. 如何正确训练一个 SVM + HOG 行人检测器

    这几个月一直在忙着做大论文,一个基于 SVM 的新的目标检测算法.为了做性能对比,我必须训练一个经典的 Dalal05 提出的行人检测器,我原以为这个任务很简单,但是我错了. 为了训练出一个性能达标的 ...

  8. 通过继承Thread类实现多线程

    (1)继承Thread类(2)重写run(方法(3)通过start0方法启动线程 一定的缺点: Java中的类是单继承的,一旦继承了Thread类,就不允许再去继承其它的类 线程和主方法之间的执行顺序 ...

  9. jsp三种注释方法

    HTML注释(输出注释):指在客户端查看源代码时能看见注释.例如, <!-- this is an html comment.it will show up int the response. ...

  10. Hadoop2.7.4 yarn(HA)集群搭建步骤(CentOS7)

    群节点分配: Park01:Zookeeper.NameNode(active).ResourceManager(active) Park02:Zookeeper.NameNode(standby) ...