1、单行子查询 :单行子查询是指子查询的返回结果只有一行数据。当主查询语句的条件语句中引用子查询结果时可用单行比较符号(=, >, <, >=, <=, <>)来进行比较。 例: select ename,deptno,sal from emp where deptno=(select deptno from dept where loc='XI AN');
ANY要符合子查询结果的任何一个值即可。而且须注意ALL 和ANY 操作符都不可以单独使用,只能与单行比较符(=、>、< 、>= 、<= 、<>)结合使用。 例: 1).多行子查询使用IN操作符: 查询选修了老师名叫Lee(假设唯一)的学生名字 sql> select stName from Student where stId in(select distinct stId from score where teId=(select teId from teacher where teName='Lee')); 查询所有部门编号为A的资料: SELECT ename,job,sal FROM EMP WHERE deptno in ( SELECT deptno FROM dept WHERE dname LIKE 'A%');
2).多行子查询使用ALL操作符:
查询有一门以上的成绩高于Lee的最高成绩的学生的名字: sql> select stName from Student where stId in(select distinct stId from score where score >all(select score from score where stId=(select stId from Student where stName= 'Lee') ));
3). 多行子查询使用ANY操作符号例子:查询有一门以上的成绩高于Lee的任何一门成绩的学生的名字: sql> select stName from Student where stId in(select distinct stId from score where score >any(select score from score where stId=(select stId from Student where stName='Lee')));