SQL statement to solve the problem of the highest salary employee in each department

The
Employee table of the employee with the highest salary in the department contains all employee information, and each employee has its corresponding Id, salary and department Id.
Insert picture description here
The Department table contains information about all departments of the company.
Insert picture description here
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.
Insert picture description here
The basic idea:

1) First query the highest salary of each department in the employee table, and get the tmp table, which contains the department id and the highest salary

2) Establish the employee table and tmp table together, query the name, department id, and maximum salary of the employee with the highest salary in the department, and get the tmp2 table.

3) Connect the department table and tmp2 table to query the department name corresponding to the department id

The second step is the key to the problem. The filter conditions here are:

1) The employee must belong to the corresponding department, so the connection condition is that the department id is equal;

2) The employee's salary is the highest in the corresponding department, so the query condition is e.salary = t.max_salary

完整的sql语句如下:
select d.name as Department,j.name as Employee ,max_salary as Salary from
department d join
(select e.name,t.did,t.max_salary from #tmp2表
employee e join
(select departmentid as did,max(salary) as max_salary from employee group by departmentid) t #tmp1表
on e.departmentid = t.did
where e.salary = t.max_salary
) j
on d.id = j.did;

All employees with the top three highest salaries in the department

The structure of the employee table and the department table is the same as the previous example.

Write a SQL query to find all the employees in each department who received the top three highest salaries. For example, according to the above given table, the query result should be returned:
Insert picture description here
Idea: In the
IT department, Max got the highest salary, Randy and Joe both got the second highest salary, and Will's salary ranked third. The sales department (Sales) has only two employees. Henry has the highest salary and Sam has the second highest salary.
Note: If there are two employees who are in the same department and get the same salary and are in the top three, they must be queried together and they only occupy one ranking.
Basic ideas:
1) Find out such employees: the number of employees who are in the same department and whose salary is higher than him does not exceed 3 (not 3), check out its name, salary, department id, and get the tmp table;
2) Link Set up the tmp and deparment tables to query the name of the department.
The sql statement is as follows:
select d.name as Department,t.name as Employee,t.Salary as Salary from
department d join
(select name,salary,departmentid from employee e1 where 3> # "number" does not exceed 3, used here count(distinct(salary)), thus ensuring that all employees with the same salary will only occupy one ranking
(select count(distinct(salary)) from employee e2
where e1.departmentid = e2.departmentid #In the same department
and e1.salary <e2.salary)) #Salary is less than the target employee
on d.id = t.departmentid
order by Department,Salary desc;

Guess you like

Origin blog.csdn.net/Dreamy_zsy/article/details/105501181