美文网首页
leetcode中的SQL题(八)

leetcode中的SQL题(八)

作者: 瓜皮小咸鱼 | 来源:发表于2019-04-03 11:03 被阅读0次

Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id

+----+-------+--------+--------------+
| Id | Name  | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1  | Joe   | 70000  | 1            |
| 2  | Henry | 80000  | 2            |
| 3  | Sam   | 60000  | 2            |
| 4  | Max   | 90000  | 1            |
+----+-------+--------+--------------+

Department 表包含公司所有部门的信息。

+----+----------+
| Id | Name     |
+----+----------+
| 1  | IT       |
| 2  | Sales    |
+----+----------+

编写一个 SQL 查询,找出每个部门工资最高的员工。例如,根据上述给定的表格,Max 在 IT 部门有最高工资,Henry 在 Sales 部门有最高工资。

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

解析:
把两个匹配的行内关联起来。

答案:

select b.name Department , a.Name  Employee, a.Salary Salary 
from Employee a inner join Department b
on a.DepartmentId = b.Id 
where a.Salary = 
(select max(Salary) from Employee where DepartmentId = b.id);

相关文章

  • leetcode中的SQL题(八)

    Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id D...

  • leetcode中的SQL题(三)

    编写一个 SQL 查询,获取 Employee 表中第 n 高的薪水(Salary)。 例如上述 Employee...

  • leetcode中的SQL题(十三)

    给定一个 salary 表,如下所示,有 m=男性 和 f=女性 的值 。交换所有的 f 和 m 值(例如,将所有...

  • leetcode中的SQL题(七)

    某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客...

  • leetcode中的SQL题(五)

    Employee 表包含所有员工,他们的经理也属于员工。每个员工都有一个 Id,此外还有一列对应员工的经理的 Id...

  • leetcode中的SQL题(十)

    编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个。 在...

  • leetcode中的SQL题(四)

    编写一个 SQL 查询来实现分数排名。如果两个分数相同,则两个分数排名(Rank)相同。请注意,平分后的下一个名次...

  • leetcode中的SQL题(九)

    Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id 。...

  • leetcode中的SQL题(十一)

    给定一个 Weather 表,编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的 Id。 ...

  • leetcode中的SQL题(十二)

    Trips 表中存所有出租车的行程信息。每段行程有唯一键 Id,Client_Id 和 Driver_Id 是 U...

网友评论

      本文标题:leetcode中的SQL题(八)

      本文链接:https://www.haomeiwen.com/subject/vujabqtx.html