部門工資最高的員工
employee 表包含所有員工資訊,每個員工有其對應的 id, salary 和 department id。 id
name
salary
departmentid
1joe
7000012
jim9000013
henry
8000024
sam6000025
max90000
1department 表包含公司所有部門的資訊。 id
name1it
2sales
編寫乙個 sql 查詢,找出每個部門工資最高的員工。對於上述表,您的 sql 查詢應返回以下行(行的順序無關緊要)。
department
employee
salary
itmax
90000
itjim
90000
sales
henry
80000
解釋:max 和 jim 在 it 部門的工資都是最高的,henry 在銷售部的工資最高。
思路
1. 首先分組查詢每個部門最高工資,用id和department欄位作為進一步查詢的關鍵鍵對;
2. 根據鍵對,查詢出每個部門最高工資員工的資訊;
3. 內連線查詢出具體的部門資訊即可;
**
select d.name as'department'
,e.name as'employee'
,e.salary
from department as d
inner join
(select employee.
*from employee
where (salary,departmentid)in(
select max
(salary)
, departmentid
from employee
group by departmentid)
)as e
on e.departmentid = d. id;
LeetCode 資料庫 部門工資最高的員工
題目描述 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 depar...
mysql 部門工資最高的員工(力扣)
sql架構 employee 表包含所有員工資訊,每個員工有其對應的 id,salary 和 department id。id name salary departmentid 1 joe 70000 1 2 jim 90000 1 3 henry 80000 2 4 sam 60000 2 5 m...
部門工資最高的員工
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...