Oracle-06

数据查询语言--DQL

1、子查询:是在一条sql语句(主查询)中,嵌套select语句(子查询)

    1)非关联子查询:嵌套的语句与主查询语句没有关联(特点:子查询可以单独去执行查询)

        eg:查询薪水最高的是谁

分步骤:
1)查询薪水最高值(子查询)
select max(salary) from emp_xu;
2)根据薪水最高值找员工(主查询)
select ename,salary from emp_xu where salary=50000;
3)合并
select ename,salary from emp_xu where salary=(salary max(salary) from emp_xu);

         注意:

             a.嵌套的select语句是放在小括号里面的

             b.非关联子查询的执行过程:

                 先执行子查询,子查询返回的结果作为主查询的条件,再去执行主查询;

                 子查询只执行一次,若子查询返回的结果为多个值时,oracle会去掉重复的,再将结果返回给主查询。

                 子查询是一条独立的语句,不依赖主查询

        eg:查询谁的薪水比'张无忌'高,表中有多个‘张无忌’

分析:
1)找‘张无忌’对应的薪水
select salary from emp_xu where ename='张无忌';
2)大于指定的薪水
select ename,salary from emp_xu where salary>();
3)合并
select ename,salary from emp_xu where salary>(select salary from emp_xu where ename='张无忌'); //错误,返回值为多个,单行子查询返回多个行
解决方案:
方案一:
select ename,salary from emp_xu where salary>all(select salary from emp_xu where ename='张无忌'); //同时大于所有(‘张无忌’)薪水
方案二:
select ename,salary from emp_xu where salary>(select max(salary) from emp_xu where ename='张无忌'); //大于张无忌最大薪水值

          注:

             1)比较运算符的选择:

                     当子查询返回一个结果,可以使用>、=、<等等;当子查询返回多个结果,不可以直接使用>,可以使用>all、>any、=any、in(=any和in可以直接进行互换)

             2)子查询结果是单列还是多列没影响,主要分清楚返回的是单行(单值)还是多行(多值);主查询不关心子查询返回的字段,只关心子查询返回的是单值还是多值

    2)关联子查询:子查询不能单独去执行

        特点:子查询不在是独立的sql语句,需要依赖主查询传递过来的参数

        关联子查询的执行过程:

               先执行主查询,将主查询的参数传递给子查询,再执行子查询,子查询执行完将结果返回给主查询,继续执行主查询,子查询是多次执行的

        eg:查询哪些员工的薪水比本部门的平均薪水值低

select ename,deptno,salary from emp_xu e
where salary<(
select avg(nvl(salary,0)) from emp_xu
where deptno=e.deptno //e.deptno表示主查询传递过来的参数
);

        eg:查询哪些人有下属

1)非关联的写法
select empno,ename from emp_xu where empno in(select leader from emp_xu);
2)关联的写法 exists关键字
select empno,ename from emp_xu e where exists(
selec 1 from emp_xu where leader=e.empno
);
注意:判断子查询有没有数据返回,有则为true,没有则为false,exists不关心返回的结果,所以子查询中select后面写什么都可以,通常用常亮“1”表示

        eg:查询哪些人没有下属

select empno,ename from emp_xu where empno not in(select leader from emp_xu where leader is not in);

           注意:not in(列表):表示判断不等于列表中所有的数据,全部满足。如果列表项中有空值,将没有结果返回(未选定行)

              in(列表):判断等于列表中任意一个数据,满足其中一个即可,如果列表项中有空,对结果没有影响

2、集合操作

    数据库中查询语句得到的就是一个结果集(ResultSet)

    eg:

集合A:{1,2,3,4,5}
集合B:{1,3,5,7,9}
集合A和集合B的合集:{1,2,3,4,5,7,9}
集合A和集合B的交集:{1,3,5}
集合A和集合B的差集:{2,4}
集合B和集合A的差集:{7,9}

      数据库中集合操作:

        前提条件:

           a. 要求两个结果集结构必须相同(当列的个数、顺序、数据类型一致时,表示两个结果集结构相同。只有结构相同的结果集才能进行集合操作)

           b. 合集:union和union all

                交集:intersect

                差集:minus

      eg:合集

select position,salary from emp_xu where deprno=10
union
select ename,salary from emp_xu where salary>6000; //union去掉重复的,同时进行排序

         错误演示:

select deptno,salary from emp_xu where deptno=10
union
select ename,salary from emp_xu where salary>6000; //错误,报数据类型不一致

select position,salary from emp_xu where deptno=10
union
select ename,salary from emp_xu where salary>6000; //没报错,但数据显示不合理
原文地址:https://www.cnblogs.com/xslzwm/p/9592687.html