Skip to content

Commit c1be75e

Browse files
committed
feature:[MySQL].185.department top three salaries
1 parent fbceedd commit c1be75e

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ LeetCode
4545
| 182 | [Duplicate Emails](https://leetcode.com/problems/duplicate-emails/description/) | Easy | [MySQL](./database/182.duplicate-emails.sql) |
4646
| 183 | [Customers Who Never Order](https://leetcode.com/problems/customers-who-never-order/) | Easy | [MySQL](./database/183.customers-who-never-order.sql) |
4747
| 184 | [Department Highest Salary](https://leetcode.com/problems/department-highest-salary/description/) | Medium | [MySQL](./database/184.department-highest-salary.sql) |
48+
| 185 | [Department Top Three Salaries](https://leetcode.com/problems/department-top-three-salaries/description/) | Hard | [MySQL](./database/185.department-top-three-salaries.sql) |
4849
| 192 | [Rising Temperature](https://leetcode.com/problems/rising-temperature/) | Easy | [MySQL](./database/197.rising-temperature.sql) |
4950
| 196 | [Delete Duplicate Emails](https://leetcode.com/problems/delete-duplicate-emails/description/) | Easy | [MySQL](./database/196.delete-duplicate-emails.sql) |
5051

Diff for: database/185.department-top-three-salaries.sql

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
--
2+
-- @lc app=leetcode id=185 lang=mysql
3+
--
4+
-- [185] Department Top Three Salaries
5+
--
6+
-- https://leetcode.com/problems/department-top-three-salaries/description/
7+
--
8+
-- database
9+
-- Hard (33.37%)
10+
-- Likes: 570
11+
-- Dislikes: 131
12+
-- Total Accepted: 79.8K
13+
-- Total Submissions: 230.5K
14+
-- Testcase Example: '{"headers": {"Employee": ["Id", "Name", "Salary", "DepartmentId"], "Department": ["Id", "Name"]}, "rows": {"Employee": [[1, "Joe", 85000, 1], [2, "Henry", 80000, 2], [3, "Sam", 60000, 2], [4, "Max", 90000, 1], [5, "Janet", 69000, 1], [6, "Randy", 85000, 1], [7, "Will", 70000, 1]], "Department": [[1, "IT"], [2, "Sales"]]}}'
15+
--
16+
-- The Employee table holds all employees. Every employee has an Id, and there
17+
-- is also a column for the department Id.
18+
--
19+
--
20+
-- +----+-------+--------+--------------+
21+
-- | Id | Name | Salary | DepartmentId |
22+
-- +----+-------+--------+--------------+
23+
-- | 1 | Joe | 85000 | 1 |
24+
-- | 2 | Henry | 80000 | 2 |
25+
-- | 3 | Sam | 60000 | 2 |
26+
-- | 4 | Max | 90000 | 1 |
27+
-- | 5 | Janet | 69000 | 1 |
28+
-- | 6 | Randy | 85000 | 1 |
29+
-- | 7 | Will | 70000 | 1 |
30+
-- +----+-------+--------+--------------+
31+
--
32+
--
33+
-- The Department table holds all departments of the company.
34+
--
35+
--
36+
-- +----+----------+
37+
-- | Id | Name |
38+
-- +----+----------+
39+
-- | 1 | IT |
40+
-- | 2 | Sales |
41+
-- +----+----------+
42+
--
43+
--
44+
-- Write a SQL query to find employees who earn the top three salaries in each
45+
-- of the department. For the above tables, your SQL query should return the
46+
-- following rows (order of rows does not matter).
47+
--
48+
--
49+
-- +------------+----------+--------+
50+
-- | Department | Employee | Salary |
51+
-- +------------+----------+--------+
52+
-- | IT | Max | 90000 |
53+
-- | IT | Randy | 85000 |
54+
-- | IT | Joe | 85000 |
55+
-- | IT | Will | 70000 |
56+
-- | Sales | Henry | 80000 |
57+
-- | Sales | Sam | 60000 |
58+
-- +------------+----------+--------+
59+
--
60+
--
61+
-- Explanation:
62+
--
63+
-- In IT department, Max earns the highest salary, both Randy and Joe earn the
64+
-- second highest salary, and Will earns the third highest salary. There are
65+
-- only two employees in the Sales department, Henry earns the highest salary
66+
-- while Sam earns the second highest salary.
67+
--
68+
--
69+
70+
-- @lc code=start
71+
-- Write your MySQL query statement below
72+
73+
SELECT d.Name AS 'Department', e1.Name AS 'Employee', e1.Salary
74+
FROM Employee e1 JOIN Department d ON e1.DepartmentId = d.Id
75+
WHERE 3 > (SELECT COUNT(DISTINCT e2.Salary)
76+
FROM Employee e2
77+
WHERE e2.Salary > e1.Salary AND e1.DepartmentId = e2.DepartmentId )
78+
79+
-- @lc code=end

0 commit comments

Comments
 (0)