部門工資最高的員工 - leetcode (中國)
select d11.department as department,e11.name as employee,d11.maxsalary as salary from (
select max(salary) as maxsalary,d1.name as department,d1.id as maxid from employee e1 left join department d1 on e1.departmentid = d1.id where d1.name is not null group by d1.name
) as d11 left join employee e11 on d11.maxsalary=e11.salary and d11.maxid=e11.departmentid
order by salary asc
employee
表包含所有員工資訊,每個員工有其對應的 id, salary 和 department id 。
+----+-------+--------+--------------+| id | name | salary | departmentid |
+----+-------+--------+--------------+
| 1 | joe | 70000 | 1 |
| 2 | henry | 80000 | 2 |
| 3 | sam | 60000 | 2 |
| 4 | max | 90000 | 1 |
| 5 | janet | 69000 | 1 |
| 6 | randy | 85000 | 1 |
+----+-------+--------+--------------+
department
表包含公司所有部門的資訊。
+----+----------+編寫乙個 sql 查詢,找出每個部門工資前三高的員工。例如,根據上述給定的**,查詢結果應返回:| id | name |
+----+----------+
| 1 | it |
| 2 | sales |
+----+----------+
+------------+----------+--------+| department | employee | salary |
+------------+----------+--------+
| it | max | 90000 |
| it | randy | 85000 |
| it | joe | 70000 |
| sales | henry | 80000 |
| sales | sam | 60000 |
+------------+----------+--------+
select d.name as department,e.name as employee,e.salary as salaryfrom employee as e
inner join department as d
on d.id = e.departmentid
where (
select count(distinct salary)
from employee as p4
where e.departmentid = p4.departmentid
and p4.salary >= e.salary
) <= 3
order by departmentid,salary desc
部門工資最高的員工
employee表包含所有員工資訊,每個員工有其對應的 id,salary 和 department id。id name salary departmentid 1 joe 70000 1 2 henry 80000 2 3 sam 60000 2 4 max 90000 1 department...
部門工資最高的員工
leecode的題目。關於in的應用。感覺很經典,這裡列出解題過程。employee 表包含所有員工資訊,每個員工有其對應的 id,salary 和 department id。sql如下 set names utf8mb4 set foreign key checks 0 table struct...
LeetCode SQL 部門工資最高的員工
employee 表包含所有員工資訊,每個員工有其對應的 id,salary 和 department id。id name salary departmentid 1 joe 70000 1 2 henry 80000 2 3 sam 60000 2 4 max 90000 1 departmen...