Oracle之子查询

文章详细介绍了SQL中的子查询用法,包括FROM后面的子查询,WHERE后面的子查询,EXISTS和NOTEXISTS的使用,以及HAVING后面的子查询。通过示例展示了如何利用子查询进行复杂的数据比较和筛选,如查询员工信息,比较课程成绩,以及查找特定部门的平均工资等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

子查询

  • 子查询是指一个查询语句嵌套在另一个查询语句内部的查询(select …)

1、from 后面的子查询

  • 将子查询的结果看作一张表,再对这张表进行查询
-- 查询员工的姓名和他上级的姓名
select e.ename, d.ename mname 
from emp e left join emp d on e.mgr = d.empno;

select e.ename, d.ename mname
from emp e left join (select empno, ename from emp) d 
			on e.mgr = d.empno;
			
-- 查询“c001”课程比“c002”课程成绩高的所有学生的学号;
select s1.sno
from (select * from sc where cno = 'c001') s1
         join (select * from sc where cno = 'c002') s2
              on s1.sno = s2.sno
where s1.score > s2.score;

-- 查询所有同学的学号、姓名、选课数、总成绩;
select s1.sno, s1.sname, s2.num 选课数, s2.sum 总成绩
from student s1 left join 
(select sno, count(cno) num, sum(score) sum from sc group by sno) s2
	on s1.sno = s2.sno;

2、where 后面的子查询

  • 将子查询的结果当作条件表达式的条件来进行比较

单行子查询

  • 单行子查询:子查询的sql语句只查出一条记录
-- 查询工资大于10号部门平均工资的员工信息
-- 查询10号部门的员工平均工资
select avg(sal) from emp where deptno = 10;

select * from emp
where sal>(select avg(sal) from emp where deptno = 10);

多行子查询

  • 多行子查询:子查询的sql 语句查出的是一个数据集合
-- 查询工资比20号部门所有人工资都高的员工信息

-- 单行子查询
select * from emp where sal>(select max(sal) from emp where deptno=20);
-- 多行子查询
select * from emp where sal>all(select sal from emp where deptno=20);

-- 查询所有课程成绩小于60 分的同学的学号、姓名;
select sno, sname from student
where sno in (select sno from sc where score < 60);

-- 查询学过“c001”并且也学过编号“c002”课程的同学的学号、姓名;
select *
from student
where sno in (select s1.sno
              from (select * from sc where cno = 'c001') s1
                       join (select * from sc where cno = 'c002') s2
                            on s1.sno = s2.sno);

-- 查询课程编号“c002”的成绩比课程编号“c001”课程低的所有同学的学号、姓名;
select sno, sname
from student
where sno in (select s1.sno
             from (select * from sc where cno = 'c001') s1
                      join (select * from sc where cno = 'c002') s2
                           on s1.sno = s2.sno
             where s1.score > s2.score);

-- 查询没有学全所有课的同学的学号、姓名;
select sno, sname from student
where sno not in (select sno from sc group by sno having count(cno) = (select count(*) from course)); 

3、exists、not exists

  • exists:后面跟一个子查询,如果子查询有结果,那么这个条件就是成立的,如果子查询无结果条件不成立
  • not exists:与exists相反
-- 查询无结果
select * from emp where exists(select * from emp where 1=0);
-- 查询emp表所有数据
select * from emp where exists(select * from emp);

-- 查询有员工的部门信息
select * from dept where deptno in (select deptno from emp group by deptno);

select * from dept d where (select count(*) from emp where deptno=d.deptno)!=0;

select * from dept d where exists(select deptno from emp where deptno=d.deptno);

-- 查询没有有员工的部门信息
select * from dept where deptno not in (select deptno from emp group by deptno);

select * from dept d where (select count(*) from emp where deptno=d.deptno)=0;

select * from dept d where not exists(select deptno from emp where deptno=d.deptno);

4、having 后面的子查询

  • 将子查询的结果当作条件表达式的条件来进行比较
-- 查询各个部门的部门编号和员工人数,要求部门的平均工资大于30号部门的平均工资

-- 查询30号部门的平均工资
select avg(sal) from emp where deptno=30;

select deptno,count(*) from emp group by deptno having avg(sal)>(select avg(sal) from emp where deptno=30);

-- 查询全部学生都选修的课程的课程号和课程名
select cno, cname from course
where cno in (select cno from sc group by cno having count(sno) = (select count(*) from student));

-- 查询没有学全所有课的同学的学号、姓名;
select sno, sname from student
where sno not in (select sno from sc group by sno having count(cno) = (select count(*) from course));

select s1.sno, s1.sname
from student s1 left join (select sno from sc group by sno having count(cno) < (select count(*) from course)) s2
on s1.sno=s2.sno;

5、select 和 from 之间的子查询

select 和 from 之间的子查询,子查询的结果只能是单行列

  • 将子查询的结果当作一个字段来进行展示
select empno, ename, (select 1 from dual) from emp;
-- 查询员工的信息及部门名称
select emp.*, (select dname from dept where deptno=emp.deptno) dname from emp;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值