SQL implementation LeetCode (185. Top three highest salaries in the department)

SQL implementation LeetCode (185. Top three highest salaries in the department)

[LeetCode] 185. Department Top Three Salaries

The Employee table holds all employees. Every employee has an Id, and there is also a column for the department Id.

+----+-------+--------+--------------+
| Id | Name | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1 | Joe | 70000 | 1 |
| 2 | Henry | 80000 | 2 |
| 3 | Sam | 60000 | 2 |
| 4 | Max | 90000 | 1 |
| 5 | Janet | 69000 | 1 |
| 6 | Randy | 85000 | 1 |
+----+-------+--------+--------------+

The Department table holds all departments of the company.

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

Write a SQL query to find employees who earn the top three salaries in each of the department. For the above tables, your SQL query should return the following rows.

+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| IT | Randy | 85000 |
| IT | Joe | 70000 |
| Sales | Henry | 80000 |
| Sales | Sam | 60000 |
+------------+----------+--------+

This question is an extension of the previous Department Highest Salary question. It is marked as Hard and is quite difficult. It combines the knowledge points of many previous questions. First, let's look at the method of using Select Count (Distinct). We merge the two tables Employee and Department, and then find out that there are only two salaries higher than the current salary. Then the top three highest salaries can be retrieved. See the code below:

Solution 1:

SELECT d.Name AS Department, e.Name AS Employee, e.Salary FROM Employee e
JOIN Department d on e.DepartmentId = d.Id
WHERE (SELECT COUNT(DISTINCT Salary) FROM Employee WHERE Salary > e.Salary
AND DepartmentId = d.Id) < 3 ORDER BY d.Name, e.Salary DESC;

The following method replaces <3 in the above method with IN (0, 1, 2), which has the same effect:

Solution 2:

SELECT d.Name AS Department, e.Name AS Employee, e.Salary FROM Employee e, Department d
WHERE (SELECT COUNT(DISTINCT Salary) FROM Employee WHERE Salary > e.Salary
AND DepartmentId = d.Id) IN (0, 1, 2) AND e.DepartmentId = d.Id ORDER BY d.Name, e.Salary DESC;

Or we can also use Group by Having Count(Distinct ..) keyword to do it:

Solution 3:

SELECT d.Name AS Department, e.Name AS Employee, e.Salary FROM 
(SELECT e1.Name, e1.Salary, e1.DepartmentId FROM Employee e1 JOIN Employee e2 
ON e1.DepartmentId = e2.DepartmentId AND e1.Salary <= e2.Salary GROUP BY e1.Id 
HAVING COUNT(DISTINCT e2.Salary) <= 3) e JOIN Department d ON e.DepartmentId = d.Id 
ORDER BY d.Name, e.Salary DESC;

The following method is slightly more complicated and uses variables. It is the same as the method used in Solution 3 in Consecutive Numbers. The purpose is to add a rank to each person according to the salary level, and finally return the items with a rank value less than or equal to 3. See the code below:

Solution 4:

SELECT d.Name AS Department, e.Name AS Employee, e.Salary FROM 
(SELECT Name, Salary, DepartmentId,
@rank := IF(@pre_d = DepartmentId, @rank + (@pre_s <> Salary), 1) AS rank,
@pre_d := DepartmentId, @pre_s := Salary 
FROM Employee, (SELECT @pre_d := -1, @pre_s := -1, @rank := 1) AS init
ORDER BY DepartmentId, Salary DESC) e JOIN Department d ON e.DepartmentId = d.Id
WHERE e.rank <= 3 ORDER BY d.Name, e.Salary DESC;

Similar topics:

Department Highest Salary

Second Highest Salary

Combine Two Tables

References:

https://leetcode.com/discuss/23002/my-tidy-solution

https://leetcode.com/discuss/91087/yet-another-solution-using-having-count-distinct

https://leetcode.com/discuss/69880/two-solutions-1-count-join-2-three-variables-join

This is the end of this article about SQL implementation of LeetCode (185. The top three highest salaries in the department). For more related SQL implementation content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • SQL implementation of LeetCode (196. Delete duplicate mailboxes)
  • SQL implementation of LeetCode (184. The highest salary in the department)
  • SQL implementation of LeetCode (183. Customers who have never placed an order)
  • SQL implementation of LeetCode (182. Duplicate mailboxes)
  • SQL implementation of LeetCode (181. Employees earn more than managers)
  • SQL implements LeetCode (180. Continuous numbers)
  • C++ implementation of LeetCode (179. Maximum number of combinations)
  • SQL implementation of LeetCode (197. Rising temperature)

<<:  Example code for implementing background blur effect with CSS

>>:  In html table, set different colors and widths for each cell

Recommend

The leftmost matching principle of MySQL database index

Table of contents 1. Joint index description 2. C...

Detailed explanation of html-webpack-plugin usage

Recently, I used html-webapck-plugin plug-in for ...

Detailed explanation of uniapp painless token refresh method

When the front-end requests the interface, it is ...

jQuery implements shopping cart function

This article example shares the specific code of ...

What are the new features of Apache Spark 2.4, which will be released in 2018?

This article is from the Apache Spark Meetup held...

Non-standard implementation code for MySQL UPDATE statement

Today I will introduce to you a difference betwee...

Analysis of the advantages of path.join() in Node.js

You might be wondering why you should use the pat...

Implementation of Vue counter

Table of contents 1. Implementation of counter 2....

About Tomcat combined with Atomikos to implement JTA

Recently, the project switched the environment an...

Super simple implementation of Docker to build a personal blog system

Install Docker Update the yum package to the late...

Practical experience of implementing nginx to forward requests based on URL

Preface Because this is a distributed file system...

Brief analysis of the MySQL character set causing database recovery errors

Importing data with incorrect MySQL character set...