update语句中使用子查询

55. 更改 108 员工的信息: 使其工资变为所在部门中的最高工资, job 变为公司中平均工资最低的 job

1). 搭建骨架

update employees set salary = (

), job_id = (


) where employee_id = 108;

2). 所在部门中的最高工资

select max(salary)
from employees
where department_id = (
select department_id
from employees
where employee_id = 108
)

3). 公司中平均工资最低的 job

select job_id
from employees
group by job_id
having avg(salary) =  (
select min(avg(salary))
from employees
group by job_id
)

4). 填充

update employees e set salary = (
select max(salary)
from employees
where department_id = e.department_id
), job_id = (
select job_id
from employees
group by job_id
having avg(salary) =  (
select min(avg(salary))
from employees
group by job_id
)
) where employee_id = 108;

56. 删除 108 号员工所在部门中工资最低的那个员工.
1). 查询 108 员工所在的部门 id

select department_id
from employees 
where employee_id = 108;

2). 查询 1) 部门中的最低工资

select min(salary)
from employees
where department_id = (
select department_id
from employees 
where employee_id = 108;
)

3). 删除 1) 部门中工资为 2) 的员工信息

delete from employees where salary = (
select min(salary)
from employees
where department_id = (
select department_id
from employees 
where employee_id = 108
)
) and department_id = (
select department_id
from employees 
where employee_id = 108
)

  

转载于:https://www.cnblogs.com/nbkyzms/p/5031436.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值