-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path184.部门工资最高的员工.sql
62 lines (60 loc) · 1.91 KB
/
184.部门工资最高的员工.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
--
-- @lc app=leetcode.cn id=184 lang=mysql
--
-- [184] 部门工资最高的员工
--
-- https://leetcode-cn.com/problems/department-highest-salary/description/
--
-- database
-- Medium (39.53%)
-- Likes: 145
-- Dislikes: 0
-- Total Accepted: 17K
-- Total Submissions: 42.2K
-- Testcase Example: '{"headers": {"Employee": ["Id", "Name", "Salary", "DepartmentId"], "Department": ["Id", "Name"]}, "rows": {"Employee": [[1, "Joe", 70000, 1], [2, "Jim", 90000, 1], [3, "Henry", 80000, 2], [4, "Sam", 60000, 2], [5, "Max", 90000, 1]], "Department": [[1, "IT"], [2, "Sales"]]}}'
--
-- 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 |
-- +------------+----------+--------+
--
--
--
# Write your MySQL query statement below
select
d.Name as Department,
e.Name as Employee,
e.Salary
from
Employee e,Department d
where
e.DepartmentId=d.id
and
(e.Salary,e.DepartmentId) in (select max(Salary),DepartmentId from Employee group by DepartmentId);