第六节----子查询

|- 如下以employees表为例:

子查询 (内查询) 在主查询之前一次执行完成。
子查询的结果被主查询(外查询)使用 。

注意:

子查询要包含在括号内。

将子查询放在比较条件的右侧。

单行操作符对应单行子查询,多行操作符对应多行子查询。
--谁的工资比Abel的高
select last_name,salary from employees where salary > (select salary from employees where last_name = 'Abel');
--查询员工名为Chen的manager的信息
select last_name,salary from employees where employee_id = (select manager_id from employees where last_name = 'Chen');
--返回job_id与141号员工相同,salary比143号员工多的员工姓名,job_id 和工资
select last_name,job_id,salary from employees where job_id=(select job_id from employees where employee_id=141)and salary >(select salary from employees where employee_id=143);
--返回公司工资最少的员工的last_name,job_id和salary
select last_name,job_id,salary from employees where salary=(select min(salary) from employees);
--查询最低工资大于50号部门最低工资的部门id和其最低工资
select department_id,min(salary) from employees group by department_id having min(salary)>(select min(salary) from employees where department_id=50);

①多行子查询
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5e0UzKfO-1610533213931)(e227f8be-2009-413b-94dc-bdbc3f7ac6c7)]

在多行子查询中使用 ANY 操作符:
--返回其它部门中比job_id为‘IT_PROG’部门任一工资低的员工的员工号、姓名、job_id 以及salary
select employee_id,last_name,job_id,salary from employees where job_id<>'IT_PROG'and salary < any(select salary from employees where job_id='IT_PROG');
在多行子查询中使用 ALL 操作符:
--返回其它部门中比job_id为‘IT_PROG’部门所有工资低的员工的员工号、姓名、job_id 以及salary
select employee_id,last_name,job_id,salary from employees where job_id<>'IT_PROG'and salary < all(select salary from employees where job_id='IT_PROG');
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值