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

mysql 8.0.15 winx64 decompression version graphic installation tutorial

Every time after installing the system, I have to...

404 error occurs when accessing the homepage of tomcat started in Docker mode

Scenario: When starting tomcat in docker (version...

JavaScript to implement drop-down list selection box

This article example shares the specific code of ...

33 ice and snow fonts recommended for download (personal and commercial)

01 Winter Flakes (Individual only) 02 Snowtop Cap...

Centos8 (minimum installation) tutorial on how to install Python3.8+pip

After minimizing the installation of Python8, I i...

Personalized and creative website design examples (30)

Therefore, we made a selection of 30 combinations ...

Why Use DOCTYPE HTML

You know that without it, the browser will use qui...

Vue implements the method of displaying percentage of echart pie chart legend

This article mainly introduces the pie chart data...

How to query the minimum available id value in the Mysql table

Today, when I was looking at the laboratory proje...

Complete steps for uninstalling MySQL database

The process of completely uninstalling the MySQL ...

CSS3 realizes the graphic falling animation effect

See the effect first Implementation Code <div ...

Research on Web Page Size

<br />According to statistics, the average s...

Summary of the pitfalls of using primary keys and rowids in MySQL

Preface We may have heard of the concept of rowid...

Solve the problem of docker container exiting immediately after starting

Recently I was looking at how Docker allows conta...

Detailed explanation of the background-position percentage principle

When I was helping someone adjust the code today,...