select d.name as 'department',e.name as 'employee', salary
from employee as e join department as d
where e.departmentId = d.id and
(e.departmentId, e.salary) in (select e.departmentId, max(e.salary) as s
from employee as e, department as d
group by e.departmentId)
with t as (select departmentId as did, max(salary) as s
from employee
group by departmentId)
select d.name as 'department',e.name as 'employee', t.s as 'salary'
from t
left join employee as e on t.s = e.salary and e.departmentId = t.did
left join department as d on t.did = d.id
先查一个部门最高的工资,然后去员工表里匹配部门号和工资

该博客探讨了一种SQL查询方法,用于从员工和部门表中找出每个部门薪水最高的员工及其详细信息。通过子查询和JOIN操作,实现了对数据的筛选和聚合,确保每个部门只显示一个最高薪资的员工记录。
735

被折叠的 条评论
为什么被折叠?



