sql子查询

sql子查询 嵌套select语句

嵌套select语句也叫子查询,一个 select 语句的查询结果能够作为另一个语句的输入值。子查询不但能够出现在where子句中,也能够出现在from子句中,作为一个临时表使用,也能够出现在select list中,作为一个字段值来返回。

1、单行子查询 :单行子查询是指子查询的返回结果只有一行数据。当主查询语句的条件语句中引用子查询结果时可用单行比较符号(=, >, <, >=, <=, <>)来进行比较。

例:
select ename,deptno,sal
from emp
where deptno>(select deptno from dept where loc='new york');

2、多行子查询:多行子查询即是子查询的返回结果是多行数据。当主查询语句的条件语句中引用子查询结果时必须用多行比较符号(in,all,any)来进行比较。其中,in的含义是匹配子查询结果中的任一个值即可("in" 操作符,能够测试某个值是否在一个列表中),all则必须要符合子查询的所有值才可,any要符合子查询结果的任何一个值即可。而且须注意all 和any 操作符不能单独使用,而只能与单行比较符(=、>、< 、>= 、<= 、<>)结合使用。

例:

1).多行子查询使用in操作符号例子:查询选修了老师名叫rona(假设唯一)的学生名字

select stname
from student
where stid in(select distinct 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的最高成绩的学生的名字:

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的任何一门成绩的学生的名字:

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、多列子查询:
当是单行多列子查询时,主查询语句的条件语句中引用子查询结果时可用单行比较符号(=, >, <, >=, <=, <>)来进行比较;
当是多行多列子查询时,主查询语句的条件语句中引用子查询结果时必须用多行比较符号(in,all,any)来进行比较。

例:
select deptno,ename,job,sal
from emp
where (deptno,sal) in (select deptno,max(sal) from emp group by deptno);

4、内联视图子查询

例:
(1)select ename,job,sal,rownum
from (select ename,job,sal from emp order by sal);

(2)select ename,job,sal,rownum
from ( select ename,job,sal from emp order by sal)
where rownum<=5;

5、在having子句中使用子查询

例:
select deptno,job,avg(sal) from emp group by deptno,job having avg(sal)>(select sal from emp where ename='martin');

让我们再看看一些具体的实例,

一、给出人口多于russia(俄国)的国家名称
select name from bbc
where population>
(select population from bbc
where name='russia')

二、给出'india'(印度), 'iran'(伊朗)所在地区的任何国家的任何信息
select * from bbc
where region in
(select region from bbc
where name in ('india','iran'))

三、给出人均gdp超过'united kingdom'(英国)的欧洲国家.
select name from bbc
where region='europe' and gdp/population >
(select gdp/population from bbc
where name='united kingdom')


sql子查询总结:

许多包含子查询的 transact-sql 语句都可以改用联接表示。在 transact-sql 中,包含子查询的语句和语义上等效的不包含子查询的语句在性能上通常没有差别。但是,在一些必须检查存在性的情况中,使用联接会产生更好的性能。否则,为确保消除重复值,必须为外部查询的每个结果都处理嵌套查询。所以在这些情况下,联接方式会产生更好的效果。
以下示例显示了返回相同结果集的select子查询和select联接:

select name  
from adventurewor ks.production.product  
where listprice =  
     (select listprice  
     from adventurewor ks.production.product  
     where name = ’chainring bolts’ )  
 

select prd1. name  
from adventurewor ks.production.product as prd1  
     join adventurewor ks.production.product as prd2  
       on (prd1.listprice = prd2.listprice)  
where prd2. name = ’chainring bolts’



嵌套在外部select语句中的子查询包括以下组件:

●包含常规选择列表组件的常规select查询。
●包含一个或多个表或视图名称的常规 from 子句。
●可选的 where 子句。
●可选的 group by 子句。
●可选的 having 子句。

子查询的select查询总是使用圆括号括起来.它不能包含compute或for browse子句,如果同时指定了 top 子句,则只能包含order by子句。

子查询可以嵌套在外部 select,insert,update 或 delete语句的 where 或 having 子句内,也可以嵌套在其他子查询内。尽管根据可用内存和查询中其他表达式的复杂程度的不同,嵌套限制也有所不同,但嵌套到 32 层是可能的。个别查询可能不支持 32 层嵌套。任何可以使用表达式的地方都可以使用子查询,只要它返回的是单个值。

如果某个表只出现在子查询中,而没有出现在外部查询中,那么该表中的列就无法包含在输出(外部查询的选择列表)中。

包含子查询的语句通常采用以下格式中的一种:

●where expression [not] in (subquery)
●where expression comparison_operator [any | all] (subquery)
●where [not] exists (subquery)

在某些 transact-sql 语句中,子查询可以作为独立查询来计算。从概念上说,子查询结果会代入外部查询(尽管这不一定是 microsoft sql server 2005 实际处理带有子查询的 transact-sql 语句的方式)。

有三种基本的子查询。它们是:

●在通过 in 或由 any 或 all 修改的比较运算符引入的列表上操作。
●通过未修改的比较运算符引入且必须返回单个值。
●通过 exists 引入的存在测试。

1.带in的嵌套查询

select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal in (select sal from scott.emp where ename=’’ward’’);
上述语句完成的是查询薪水和ward相等的员工,也可以使用not in来进行查询。

2.带any的嵌套查询
通过比较运算符将一个表达式的值或列值与子查询返回的一列值中的每一个进行比较,只要有一次比较的结果为true,则any测试返回true。
select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal >any(select sal from scott.emp where job=’’manager’’);
等价于下边两步的执行过程:
(1)执行“select sal from scott.emp where job=’’manager’’”
(2)查询到3个薪水值2975、2850和2450,父查询执行下列语句:
select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal >2975 or sal>2850 or sal>2450;



3.带some的嵌套查询
select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal =some(select sal from scott.emp where job=’’manager’’);
等价于下边两步的执行过程:
(1)子查询,执行“select sal from scott.emp where job=’’manager’’”。
(2)父查询执行下列语句。
select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal =2975 or   sal=2850 or sal=2450;
带【any】的嵌套查询和【some】的嵌套查询功能是一样的。早期的sql仅仅允许使用【any】,后来的版本为了和英语的【any】相区分,引入了【some】,同时还保留了【any】关键词。

4.带all的嵌套查询
通过比较运算符将一个表达式的值或列值与子查询返回的一列值中的每一个进行比较,只要有一次比较的结果为false,则all测试返回false。
select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal >all(select sal from scott.emp where job=’’manager’’);
等价于下边两步的执行过程:
(1)子查询,执行“select sal from scott.emp where job=’’manager’’”。
(2)父查询执行下列语句。
select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal >2975 and sal>2850 and sal>2450;

5.带exists的嵌套查询

select emp.empno,emp.ename,emp.job,emp.sal from scott.emp,scott.dept where exists (select * from scott.emp where scott.emp.deptno=scott.dept.deptno);

6.并操作的嵌套查询

并操作就是集合中并集的概念。属于集合a或集合b的元素总和就是并集。
(select deptno from scott.emp) union (select deptno from scott.dept);

7.交操作的嵌套查询

交操作就是集合中交集的概念。属于集合a且属于集合b的元素总和就是交集。

(select deptno from scott.emp) intersect (select deptno from scott.dept);

8.差操作的嵌套查询

差操作就是集合中差集的概念。属于集合a且不属于集合b的元素总和就是差集。
(select deptno from scott.dept) minus (select deptno from scott.emp);
注意:并、交和差操作的嵌套查询要求属性具有相同的定义,包括类型和取值范围。

左手边是一个标量表达式列表.右手边可以是一个等长的标量表达式的列表,或者一个圆括弧括起来的子查询,该查询必须返回很左手边表达式书目完全一样的字段.另外,该子查询不能返回超过一行的数量.(如果它返回零行, 那么结果就是 null.)左手边逐行与右手边的子查询结果行,或者右手边 表达式列表进行比较.目前,只允许使用 = 和 <> 操作符进行逐行比较.如果两行分别是相等或者不等,那么结果为真.

通常,表达式或者子查询行里的 null 是按照 sql 布尔表达式的一般规则进行组合的.如果两个行对应的成员都是非空并且相等,那么认为这两行 相等;如果任意对应成员为非空且不等,那么该两行不等;否则这样的行比较的结果是未知(null).


请使用浏览器的分享功能分享到微信等