[LeetCode] 185. Department Top Three SalariesThe Employee table holds all employees. Every employee has an Id, and there is also a column for the department Id.
The Department table holds all departments of the company.
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.
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:
|
<<: Example code for implementing background blur effect with CSS
>>: In html table, set different colors and widths for each cell
Table of contents 1. Joint index description 2. C...
Recently, I used html-webapck-plugin plug-in for ...
Preface: I have always wanted to know how a SQL s...
When the front-end requests the interface, it is ...
Overview binlog2sql is an open source MySQL Binlo...
This article example shares the specific code of ...
This article is from the Apache Spark Meetup held...
Today I will introduce to you a difference betwee...
You might be wondering why you should use the pat...
Table of contents 1. Implementation of counter 2....
Recently, the project switched the environment an...
Install Docker Update the yum package to the late...
Preface Because this is a distributed file system...
Importing data with incorrect MySQL character set...
background In order to support Docker containeriz...