当前位置:Gxlcms > 数据库问题 > Oracle【子查询】

Oracle【子查询】

时间:2021-07-01 10:21:17 帮助过:13人阅读


单行子查询 :
筛选条件不明确,需要执行一次查询且查询结果只有一个字段且字段值只有一个。
注意:where子句中允许出现查询语句,该查询语句称为子查询。
使用:select 内容 from 表名 where 字段名 比较运算符 子查询语句

 1 --查询所有比雇员‘CLARK‘工资高于员工的信息
 2 select * from emp where sal>(select sal from emp where ename=CLARK);
 3 --查询工资高于平均工资的员工的名字和工资
 4 select ename,sal from emp where sal>(select avg(sal) from emp);
 5 --查询和soctt属于同一部门且工资比他低的员工资料
 6 select * from emp where deptno=(select deptno from emp where ename=SCOTT) and sal<(select sal from emp where ename=SCOTT);
 7 --查询工资最高的员工资料
 8 select * from emp where sal=(select max(sal) from emp);
 9 --查询职务和SCOTT相同,雇佣时间早的员工信息
10 select * from emp where job=(select job from emp where ename=SCOTT) and hiredate < (select hiredate from emp where ename=SCOTT);
11 --查询工资比SCOTT高或者雇佣时间早的员工编号和姓名
12 select empno,ename from emp where sal>(select sal from emp where ename=SCOTT) or hiredate < (select hiredate from emp where ename=SCOTT);

多行子查询:
子查询的结果只有一个字段但是字段有n个值,考虑使用多行子查询,其实使用关键字

关键字1:any 任意
  select 内容 from 表名 where 字段名 比较运算符 any 子查询语句
关键字2:all 所有
  select 内容 from 表名 where 字段名 比较运算符 all 子查询语句
关键字3:in 表示任意存在,相当于 = any 
  select 内容 from 表名 where 字段名 in 子查询语句 
  select 内容 from 表名 where 字段名 not in 子查询语句

1 --查询工资高于任意一个CLERK的所有员工信息
2 select * from emp where sal> any (select sal from emp where job=CLERK);
3 --查询工资高于所有的SALESMAN的员工信息
4 select * from emp where sal> all (select sal from emp where job=SALESMAN);
5 --查询部门20中同部门10的雇员工作一样的雇员信息
6 select * from emp where job  in (select job from emp where deptno=10) and deptno=20;

 

Oracle【子查询】

标签:资料   job   需要   div   允许   red   运算符   比较   查询   

人气教程排行