SQL嵌套SELECT语句是很常见的SQL语句,下面就为您详细介绍SQL嵌套SELECT语句的语法,并附相关示例,供您参考学习之用。
嵌套SELECT语句也叫子查询,一个SELECT 语句的查询结果能够作为另一个语句的输入值。子查询不但能够出现在Where子句中,也能够出现在from子句中,作为一个临时表使用,也能够出现在select list中,作为一个字段值来返回。
1、单行子查询 :单行子查询是指子查询的返回结果只有一行数据。当主查询语句的条件语句中引用子查询结果时可用单行比较符号(=, >, =, 、< 、>= 、 select stName from Student where stId in(selectdistinct stId from score where teId=(select teId from teacher where teName='Rona')); 查询所有部门编号为A的资料:
SELECT ename,job,sal FROM EMP WHERE deptno in ( SELECT deptno FROM dept WHERE dname LIKE 'A%'); 2).多行子查询使用ALL操作符号例子:查询有一门以上的成绩高于Kaka的最高成绩的学生的名字:
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= 'Kaka') )); 3). 多行子查询使用ANY操作符号例子:查询有一门以上的成绩高于Kaka的任何一门成绩的学生的名字:
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='Kaka'))); 3、多列子查询:当是单行多列的子查询时,主查询语句的条件语句中引用子查询结果时可用单行比较符号(=, >, =,