Skip to content

Commit c806065

Browse files
committed
feature:[MySQL].184.department highest salary
1 parent 5ee675c commit c806065

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ LeetCode
4343
| 181 | [Employees Earning More Than Their Managers](https://leetcode.com/problems/employees-earning-more-than-their-managers/description/) | Easy | [MySQL](./database/181.employees-earning-more-than-their-managers.sql) |
4444
| 182 | [Duplicate Emails](https://leetcode.com/problems/duplicate-emails/description/) | Easy | [MySQL](./database/182.duplicate-emails.sql) |
4545
| 183 | [Customers Who Never Order](https://leetcode.com/problems/customers-who-never-order/) | Easy | [MySQL](./database/183.customers-who-never-order.sql) |
46+
| 184 | [Department Highest Salary](https://leetcode.com/problems/department-highest-salary/description/) | Medium | [MySQL](./database/184.department-highest-salary.sql) |
4647
| 192 | [Rising Temperature](https://leetcode.com/problems/rising-temperature/) | Easy | [MySQL](./database/197.rising-temperature.sql) |
4748
| 196 | [Delete Duplicate Emails](https://leetcode.com/problems/delete-duplicate-emails/description/) | Easy | [MySQL](./database/196.delete-duplicate-emails.sql) |
4849

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
--
2+
-- @lc app=leetcode id=184 lang=mysql
3+
--
4+
-- [184] Department Highest Salary
5+
--
6+
-- https://leetcode.com/problems/department-highest-salary/description/
7+
--
8+
-- database
9+
-- Medium (35.76%)
10+
-- Likes: 480
11+
-- Dislikes: 112
12+
-- Total Accepted: 111.4K
13+
-- Total Submissions: 303K
14+
-- 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"]]}}'
15+
--
16+
-- The Employee table holds all employees. Every employee has an Id, a salary,
17+
-- and there is also a column for the department Id.
18+
--
19+
--
20+
-- +----+-------+--------+--------------+
21+
-- | Id | Name | Salary | DepartmentId |
22+
-- +----+-------+--------+--------------+
23+
-- | 1 | Joe | 70000 | 1 |
24+
-- | 2  | Jim   | 90000  | 1            |
25+
-- | 3 | Henry | 80000 | 2 |
26+
-- | 4 | Sam | 60000 | 2 |
27+
-- | 5 | Max | 90000 | 1 |
28+
-- +----+-------+--------+--------------+
29+
--
30+
--
31+
-- The Department table holds all departments of the company.
32+
--
33+
--
34+
-- +----+----------+
35+
-- | Id | Name |
36+
-- +----+----------+
37+
-- | 1 | IT |
38+
-- | 2 | Sales |
39+
-- +----+----------+
40+
--
41+
--
42+
-- Write a SQL query to find employees who have the highest salary in each of
43+
-- the departments. For the above tables, your SQL query should return the
44+
-- following rows (order of rows does not matter).
45+
--
46+
--
47+
-- +------------+----------+--------+
48+
-- | Department | Employee | Salary |
49+
-- +------------+----------+--------+
50+
-- | IT | Max | 90000 |
51+
-- | IT         | Jim      | 90000  |
52+
-- | Sales | Henry | 80000 |
53+
-- +------------+----------+--------+
54+
--
55+
--
56+
-- Explanation:
57+
--
58+
-- Max and Jim both have the highest salary in the IT department and Henry has
59+
-- the highest salary in the Sales department.
60+
--
61+
--
62+
63+
-- @lc code=start
64+
-- Write your MySQL query statement below
65+
66+
SELECT b.Name AS Department, a.Name AS Employee, Salary
67+
FROM Employee a JOIN Department b ON a.DepartmentId = b.Id
68+
WHERE (a.DepartmentId, a.Salary
69+
) IN
70+
(SELECT
71+
DepartmentId, MAX(Salary)
72+
FROM
73+
Employee
74+
GROUP BY DepartmentId)
75+
76+
-- @lc code=end

0 commit comments

Comments
 (0)