You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
createtableemployee (
id int,
name varchar(255),
salary int,
departmentId int
);
insert into employee values
(1,'Joe', 70000, 1),
(2,'Jim', 90000, 1),
(3,'Henry', 80000, 2),
(4,'Sam', 60000, 2),
(5,'Max', 90000, 1);
createtabledepartment(id int, name varchar(255));
insert into department values(1, 'it'), (2, 'sales');
SQL:方法一
selectdepartment.name, employee.name, salary from employee
join department onemployee.departmentId=department.idwhere (departmentId, salary) in (
select departmentId, max(salary) from employee group by departmentId
);
解析
两个字段也可以用 in 。
SQL:方法二
with temp as (
selectdepartment.nameas department, employee.nameas employee, salary,
dense_rank() over(partition by departmentId order by salary desc) as 排名
from employee left join department onemployee.departmentId=department.id
) select department, employee, salary from temp where 排名 =1;
解析
使用 with 建立临
连接两表 department 和 employee 使用 dense_rank() 对 salary 进行排序。
partition by 的作用是分区
The text was updated successfully, but these errors were encountered:
题目
找出每个部门工资最高的员工。
SQL:方法一
解析
两个字段也可以用
in
。SQL:方法二
解析
with
建立临department
和employee
使用dense_rank()
对salary
进行排序。partition by
的作用是分区The text was updated successfully, but these errors were encountered: