码迷,mamicode.com
首页 > 数据库 > 详细

oracle 子查询

时间:2017-10-04 14:32:48      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:无法   red   最大   oct   操作   dep   返回   子查询   soc   


    6-1:子查询简介
    6-2:
        select * from scott.emp
        where sal=(select MIN(sal) from scott.emp);
      范例:查找出公司雇佣最早的雇员;
        select min(hiredate) from scott.emp
      以上查询会返回单行单列的数据,所有可以直接在where语句中使用.
        select * from scott.emp
        where hiredate=(select min(hiredate) from scott.emp);
  6.1.2、子查询返回单行多列;
        范例:查询出与scott工资相同,职位相同的所有雇员信息。
          
        select * from scott.emp where (sal,job)=(select sal,job from scott.emp where ename=‘SCOTT‘);

  6.1.2.1:  找出部门10中所有经理和部门20中的所有办事员的详细资料
            select * from emp where (deptno=10 and job=upper(‘manager‘)) or (deptno=20 and job=upper(‘clerk ‘));
            找出不收取佣金或收取的佣金低于100的雇员
            select * from emp where nvl(comm,0)<100;
            找出各月最后一天受雇的所有雇员
              select * from emp where hiredate= last_day(hiredate);
            显示不带有‘R‘的雇员姓名
              Select ename from emp where ename not like ‘%R%’;

  6.1.3 子查询返回多行单列(重点)
        在where字句里面提供有主要的三个运算符:IN、ANY、ALL。

      1:IN操作;
          IN操作指的是内容可以在指定的范围之中存在,
        1.1:select sal from scott.emp where job=‘MANAGER’;
              2975
              2850
              2450
        1.2:select * from scott.emp
              WHERE (sal,job)=(
                      select sal,job FROM scott.emp where ename=‘SCOTT‘);

              多行单列就相当于给出了一个查询范围;
        对于IN操作还可以使用not IN 进行;
            select * from scott.emp
              where sal  NOT IN  (select sal from scott.emp where job=‘MANAGER’);
      select ename,sal from scott.emp where sal >(select sal from scott.emp where ename=‘BLAKE‘);
      2:ANY操作实际上有三种语法:
          1:=ANY:功能上与IN完全是没有任何区别;
            select * from scott.emp
              where sal  =ANY  (select sal from scott.emp where job=‘MANAGER’);
          2:>ANY:比子查询返回的最小的内容要大
            select * from scott.emp
              where sal  >ANY  (select sal from scott.emp where job=‘MANAGER’);
          3:<ANY:比子查询返回的最大的值要小
            select * from scott.emp
              where sal  <ANY  (select sal from scott.emp where job=‘MANAGER’);
      3:ALL操作
          ALL两种操作
          1:>ALL: 比子查询返回的最大值要大
              select * from scott.emp
              where sal  >ALL  (select * from scott.emp where job=‘MANAGER’);
          2:<ALL: 比子查询的返回最小的值要小
               select * from scott.emp
              where sal  <ALL  (select * from scott.emp where job=‘MANAGER’);
      4:EXISTS()判断
          范例:观察exists()操作
            select * from scott.emp
              where exists(select * from scott.emp where deptno=99);
            因为此时的查询没有返回任何的数据行买,所以exists()就认为数据不存在,外部查询无法查询出我内容。
          范例:观察exists()操作
            select * from scott.emp
              where exists(select * from scott.emp where empno=7839);
          范例:观察exists()操作
            select * from scott.emp
              where exists (select ‘hello’ from dual where 1=1);
          范例:使用not exists()
            select * from scott.emp
              where not exists (select ‘hello’ from dual where 1=2);
6-3:使用HAVING字句查询;
    范例:要求统计出高于公司平均工资的部门编号、平均工资、部门人数。
    第一步:根据部门编号分组,统计出每个部门编号的平均工资、部门人数。
      select deptno,count(*),avg(sal)
        from scott.emp
        group by deptno;
    第二步:如果要想知道哪些部门的工资高于公司的平均工资,则应该scott.emp表统计查询
      select avg(sal) from scott.emp;
    第三步:统计函数
      select deptno,count(*),avg(sal)
        from scott.emp
        group by deptno
        having avg(sal)>(select avg(sal) from scott.emp);
6-4:select 子句使用子查询
  首先需要明确的是,这样的操作意义不大,而且性能不高;
    范例:查询每个雇员的编号,姓名,职位,部门名称;
      select e.empno,e.ename,e.job,d.dname
        from scott.emp e,soctt.emp d
        where e.deptno=d.deptno;

oracle 子查询

标签:无法   red   最大   oct   操作   dep   返回   子查询   soc   

原文地址:http://www.cnblogs.com/Juvenile/p/7625333.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!