leetcode184. highest paid employee department (SQL) connection + nested query

Employee table contains information on all employees, each has its corresponding employee Id, salary and department Id.

---- + -------- + ------- + + -------------- +
| Id | the Name | the Salary | DepartmentId |
+ - - + -------- + ------- + -------------- +
| 1 | Joe | 70000 | 1 |
| 2 | Henry | 80000 | 2 |
| 3 | Sam | 60000 | 2 |
| 4 | Max | 90000 | 1 |
+ ---- + -------- + ------- + ------ + --------
department table contains information about all sectors of the company.

+ ---------- + ---- +
| Id | the Name |
+ ---- + ---------- +
| 1 | IT |
| 2 | Sales |
+ ---- + ---------- +
write a SQL query to find the highest wages of employees in each department. For example, according to the above given table, Max had the highest wages in the IT department, Henry had the highest wages in the Sales department.

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

Do connect the two tables, the conditions are the same department id and wage maximum wage equal to the nested query out.

Published 552 original articles · won praise 10000 + · views 1.32 million +

Guess you like

Origin blog.csdn.net/hebtu666/article/details/104322273