[LeetCode] 184. Department Highest SalaryThe Employee table holds all employees. Every employee has an Id, a salary, 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 have the highest salary in each of the departments. For the above tables, Max has the highest salary in the IT department and Henry has the highest salary in the Sales department.
This question gives us two tables, Employee and Department, and asks us to find the person with the highest salary in the department. In fact, this question is a combination of Second Highest Salary and Combine Two Tables . We need to combine the two tables and find the highest salary. So we first intersect the two tables, then mark the required columns in the result table, and then find the highest salary. We use the Max keyword to achieve this. See the code below: Solution 1: SELECT d.Name AS Department, e1.Name AS Employee, e1.Salary FROM Employee e1 JOIN Department d ON e1.DepartmentId = d.Id WHERE Salary IN (SELECT MAX(Salary) FROM Employee e2 WHERE e1.DepartmentId = e2.DepartmentId); We can also use Where to join the two tables without using the Join keyword, and then find the highest salary in the same way as above: Solution 2: SELECT d.Name AS Department, e.Name AS Employee, e.Salary FROM Employee e, Department d WHERE e.DepartmentId = d.Id AND e.Salary = (SELECT MAX(Salary) FROM Employee e2 WHERE e2.DepartmentId = d.Id); The following method does not use the Max keyword, but uses the >= symbol, which achieves the same effect as the Max keyword. See the code below: Solution 3: SELECT d.Name AS Department, e.Name AS Employee, e.Salary FROM Employee e, Department d WHERE e.DepartmentId = d.Id AND e.Salary >= ALL (SELECT Salary FROM Employee e2 WHERE e2.DepartmentId = d.Id); Similar topics: Second Highest Salary Combine Two Tables This is the end of this article about SQL implementation of LeetCode (184. The highest salary in the department). For more relevant content about SQL implementation of the highest salary in the department, 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:
|
<<: How to match the size of text in web design: small text, big experience
>>: Website background music implementation method
Recently, when I was learning docker, I found tha...
binlog is a binary log file that records all DML ...
IIS7 needs to confirm whether the "URL REWRI...
Specific method: Step 1: Stop the mysql service /...
The Docker images we usually build are usually la...
1. Basic Concepts 1. Sitemesh is a page decoratio...
Due to the advantages of GTID, we need to change ...
Preface Everyone knows that the partition field m...
When using MySql's window function to collect...
This week has been as busy as a war. I feel like ...
If you use docker search centos in Docker Use doc...
In the project (nodejs), multiple data need to be...
In this article, the blogger will take you to lea...
Achieve results Code html <div class="css...
When we edit a layout, we usually use horizontal ...