LeetCode force buckle brush title database (184): The highest employee sector wages

topic

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

Here Insert Picture Description
Department table contains information about all sectors of the company.

Here Insert Picture Description
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.
Here Insert Picture Description

analysis

-- 查看员工表
SELECT
	* 
FROM
	employee;

Here Insert Picture Description

-- 查看部门表
SELECT
	* 
FROM
	department;

Here Insert Picture Description

-- 每个部门的最大薪水
SELECT
	DepartmentId,
	MAX( Salary ) 
FROM
	Employee 
GROUP BY
	DepartmentId;
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200225113606400.png)
-- 连接员工表和部门表
SELECT
	* 
FROM
	Employee
	JOIN department ON Employee.DepartmentId = Department.Id;

Here Insert Picture Description

-- 加上前面那个部门薪水最高的表过滤一下
SELECT
	* 
FROM
    Employee
        JOIN
    Department ON Employee.DepartmentId = Department.Id
WHERE
    (Employee.DepartmentId , Employee.Salary) IN
    (   SELECT
            DepartmentId, MAX(Salary)
        FROM
            Employee
        GROUP BY DepartmentId
	)
;

Here Insert Picture Description

-- 列名
SELECT
	Department.Name as Department,
	Employee.Name as Employee,
	Salary
FROM
    Employee
        JOIN
    Department ON Employee.DepartmentId = Department.Id
WHERE
    (Employee.DepartmentId , Employee.Salary) IN
    (   SELECT
            DepartmentId, MAX(Salary)
        FROM
            Employee
        GROUP BY DepartmentId
	)
;

Here Insert Picture Description

answer

-- 查看员工表
SELECT
	* 
FROM
	employee;
	
-- 查看部门表
SELECT
	* 
FROM
	department;
	
-- 每个部门的最大薪水
SELECT
	DepartmentId,
	MAX( Salary ) 
FROM
	Employee 
GROUP BY
	DepartmentId;
	
-- 连接员工表和部门表
SELECT
	* 
FROM
	Employee
	JOIN department ON Employee.DepartmentId = Department.Id;

-- 加上前面那个部门薪水最高的表过滤一下
SELECT
	* 
FROM
    Employee
        JOIN
    Department ON Employee.DepartmentId = Department.Id
WHERE
    (Employee.DepartmentId , Employee.Salary) IN
    (   SELECT
            DepartmentId, MAX(Salary)
        FROM
            Employee
        GROUP BY DepartmentId
	)
;

	
	
-- 列名
SELECT
	Department.Name as Department,
	Employee.Name as Employee,
	Salary
FROM
    Employee
        JOIN
    Department ON Employee.DepartmentId = Department.Id
WHERE
    (Employee.DepartmentId , Employee.Salary) IN
    (   SELECT
            DepartmentId, MAX(Salary)
        FROM
            Employee
        GROUP BY DepartmentId
	)
;



related business

Within six months

from

Six months to 1 year

Here Insert Picture Description

1 year to 2 years

Here Insert Picture Description

Published 527 original articles · won praise 651 · views 80000 +

Guess you like

Origin blog.csdn.net/qq_35456045/article/details/104494078