Buckle 184. The highest paid employee in the department

Buckle 184. The highest paid employee in the department

https://leetcode-cn.com/problems/department-highest-salary/

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

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

+ ---- + ---------- +
| Id | Name |
+ ---- + ---------- +
| 1 | IT |
| 2 | Sales |
+ ---- + ---------- +
Write a SQL query to find the highest paid employee in each department. For example, according to the table given above, 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  |
+------------+----------+--------+

 

Idea: Use where subquery and in statement

select Department.name as Department,
        Employee.name as Employee,
        Employee.Salary
from Employee join Department on Employee.DepartmentId=Department.Id#连接,组合
where (Employee.DepartmentId,Employee.Salary) in #要用IN不能用=
(   #以此在部门内查询最高工资
    select Employee.DepartmentId,max(Employee.Salary)
    #注意:有可能有多个员工同时拥有最高工资,所以最好在这个查询中不包含雇员名字的信息。
    from Employee
    group by Employee.DepartmentId#
)
;

 

Attached knowledge points:

1. Generally, the maximum and minimum problem is to use where subquery

 

2. Reasons for using join:

Both tables may be empty, so left and right cannot be left, only join

Use left join to note that the department name cannot be NULL. So, if you use Left join to add a sentence after the official answer AND Department.name is not null

 

Different SQL JOIN

In addition to the INNER JOIN we used in the example above, we can also use several other connections.

The types of JOIN you can use are listed below and the differences between them.

  • JOIN: If there is at least one match in the table, then return the row
  • LEFT JOIN: Even if there is no match in the right table, all rows are returned from the left table
  • RIGHT JOIN: Even if there is no match in the left table, all rows are returned from the right table
  • FULL JOIN: As long as there is a match in one of the tables, return the row
SELECT
    Department.name AS 'Department',
    Employee.name AS 'Employee',
    Salary
FROM
    Employee
LEFT JOIN
    Department ON Employee.DepartmentId = Department.Id 
WHERE
    (Employee.DepartmentId , Salary) IN
    (   SELECT
            DepartmentId, MAX(Salary)
        FROM
            Employee
        GROUP BY DepartmentId
	)
AND Department.name is not null
;

3. IN operator

The IN operator allows us to specify multiple values ​​in the WHERE clause.

IN syntax

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...)

example:

The original table (used in the example :)

Persons table:

IN operator example

Now, we want to select people whose surnames are Adams and Carter from the above table:

We can use the following SELECT statement:

SELECT * FROM Persons
WHERE LastName IN ('Adams','Carter')

Published 23 original articles · praised 0 · visits 137

Guess you like

Origin blog.csdn.net/qq_35683407/article/details/105428480