时间:2021-07-01 10:21:17 帮助过:51人阅读
创建Department 表,包含公司所有部门的信息。
+----+----------+
| Id | Name |
+----+----------+
| 1 | IT |
| 2 | Sales |
+----+----------+
-- 创建department表 CREATE TABLE department ( id int(11) NOT NULL, name varchar(100) NOT NULL, PRIMARY KEY (`id`)); -- 插入数据 INSERT INTO `department` VALUES (‘1‘, ‘IT‘); INSERT INTO `department` VALUES (‘2‘, ‘Sales‘); INSERT INTO `department` VALUES (‘3‘, ‘customs‘);
编写一个 SQL 查询,找出每个部门工资最高的员工。例如,根据上述给定的表格,Max 在 IT 部门有最高工资,Henry 在 Sales 部门有最高工资。
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| Sales | Henry | 80000 |
+------------+----------+--------+
注意点:需要考虑同部门最高工资有多个人的情况
SELECT e.*,t.name department,t.maxsal FROM employee e INNER JOIN (SELECT e.departmentid,d.name,MAX(salary) maxsal FROM employee e INNER JOIN department d on e.departmentid = d.id GROUP BY e.departmentid,d.name) t on e.departmentid = t.departmentid WHERE e.salary=t.maxsal
MySQL_项目7: 各部门工资最高的员工(难度:中等)
标签:char 根据 arc class int str ane mys 难度