LeetCode:. 184 highest-paid employees department

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

topic

Employee The following table contains information about all employees, each employee has its corresponding 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 |
+------------+----------+--------+

Source: stay button (LeetCode)
link: https://leetcode-cn.com/problems/department-highest-salary
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

answer

Haha, the first time on debugging debugging through.

---- oracle ----
/* Write your PL/SQL query statement below */
select b.Name as Department,
       t.Name as Employee,
       t.Salary as Salary
from Department b
left join
(
select Name,
       Salary,
       DepartmentId,
       rank() over(partition by DepartmentId order by Salary desc) as rank
from Employee
) t
on t.DepartmentId = b.Id
where t.rank = 1; ---- 791ms

By observing the situation by testing samples and found their own problems of the code.

Use a start row_number()time to sort and find the same salary will get different rankings, does not meet the requirements of the title, so instead rank()through.

The first time is not passed, we found the presence of test cases Departmentexist table empty, so the left jointwo tables exchange position, they can.

Use joinand inanswers.

---- MySQL ----
select b.name as Department,
       a.name as Employee,
       Salary
from Employee a
join Department b
on a.DepartmentId = b.Id
where (a.DepartmentId, Salary) in
(
select DepartmentId,
       max(Salary) as Salary
from Employee
group by DepartmentId
); ---- 221ms

Using the left joinsame incorrect report, because there is a sample did not pass, change joinor directly inner joincan.

Think

Step by step, thinking, thinking, thinking the most important!

MySQLIn inmay be two fields together.

select xxx
from yyy
where (a, b) in (c, d);

Overall, still I started using the rank()function of the most beautiful, ha ha!

Subsequent to sum up oraclethe use of four sorting function.

Guess you like

Origin www.cnblogs.com/hider/p/11746419.html