SQL implementation of LeetCode (184. The highest salary in the department)

SQL implementation of LeetCode (184. The highest salary in the department)

[LeetCode] 184. Department Highest Salary

The Employee table holds all employees. Every employee has an Id, a salary, 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 |
+----+-------+--------+--------------+

The Department table holds all departments of the company.

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

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.

+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| Sales | Henry | 80000 |
+------------+----------+--------+

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:
  • SQL implementation of LeetCode (196. Delete duplicate mailboxes)
  • SQL implementation LeetCode (185. Top three highest salaries 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)

<<:  How to match the size of text in web design: small text, big experience

>>:  Website background music implementation method

Recommend

Introducing ECharts into the Vue project

Table of contents 1. Installation 2. Introduction...

CocosCreator Universal Framework Design Network

Table of contents Preface Using websocket Constru...

XHTML Web Page Tutorial

<br />This article is mainly to let beginner...

MySQL database operation and maintenance data recovery method

The previous three articles introduced common bac...

Analysis of log files in the tomcat logs directory (summary)

Each time tomcat is started, the following log fi...

Who is a User Experience Designer?

Scary, isn't it! Translation in the picture: ...

Node and Python two-way communication implementation code

Table of contents Process Communication Bidirecti...

MySQL master-slave principle and configuration details

MySQL master-slave configuration and principle, f...

Basic operation tutorial of files and permissions in centos

Preface Before we begin, we should briefly unders...

A brief summary of my experience in writing HTML pages

It has been three or four months since I joined Wo...

Two ways to prohibit clearing the input text input cache in html

Most browsers will cache input values ​​by defaul...

How to use squid to build a proxy server for http and https

When we introduced nginx, we also used nginx to s...

Sample code for changing the color of a png image through a CSS3 filter

This method uses the drop-shadow filter in CSS3 t...