zoukankan      html  css  js  c++  java
  • 子查询

    子查询
    比SCOTT工资高的 员工信息
    1:scott的工资->5200
    select SAL from emp where ename = 'SCOTT' ;

    2.>5200的员工信息
    select *from emp where sal > ;
    ->合二为一

    select *from emp where sal > (select SAL from emp where ename = 'SCOTT' )

    ①子查询可以出现的位置:where、select、having、from ;不能写在group by 后面。
    where
    select: 单行列 (常量列)
    select empno 第一列,ename 第二列,(select job from emp where empno =7369) 第三列 from emp ;
    having:
    查询最低工资比10号部门的最低工资高的部门编号。

    分组
    select deptno,min(sal) from emp
    group by deptno
    having min(sal) > ( select min(sal) from emp where deptno =10 );

    from:相当于修改了表结构
    select * from emp;
    select * from ( select empno,ename, sal*12 from emp) ;

    ②主查询和子查询 可以是,也可以不同同一张表

    查询销售部的员工信息
    1.现根据“销售部”查询 销售部的部门编号30
    select deptno from dept where dname = 'DNAME' ;
    2.根据部门编号30 查询员工信息
    select * from emp where deptno = (select deptno from dept where dname = 'SALES' );

    ③子查询可以使用 单行操作符(=,<),多行操作符(in)
    查询工资比30号部门中 任意其中一个员工高的(存在) 员工信息

    "只需要满足一个即可,存在一个就可以"->any
    select *from emp where sal > any(select sal from emp) ;

    select *from emp where sal > (select min(sal) from emp) ;


    查询工资比30号部门中 全部员工高的(存在) 员工信息
    “所有、全部”->all
    select *from emp where sal > all(select sal from emp) ;
    select *from emp where sal > (select max(sal) from emp) ;

    多行操作符:
    查询销售部,财务部的员工信息
    select * from emp where deptno in
    (select deptno from dept where dname = 'SALES' or dname='ACCOUNTING');

    any:只要有一个
    all:全部

    ④子查询中的null :子查询的结果中不要有NULL!!
    select *from emp where mgr in (7566,7698);
    select *from emp where mgr in (7566,7698,NULL);

    in : = or = or
    select *from emp where mgr =7566 or mgr=7698 or mgr = NULL;

    select *from emp where mgr not in (7566,7698,NULL);


    select *from emp where mgr !=7566 and mgr!=7698 and mgr != NULL ;


    NULL:自身特性: 如果!=NULL则无法查询出任何数据


    is null
    is not null

    =null
    != null

    查询 不是领导的员工信息(子查询时排除NULL)

    不是领导:判断empno 是否存在于mgr中
    select * from emp
    where empno not in (select mgr from emp where mgr is not null )

    ⑤一般不在子查询中排序,除非TOP-N问题(分页)

  • 相关阅读:
    c++11 static_assert
    UVA
    析构函数与运行错误
    UVA
    2017 ACM-ICPC 亚洲区(乌鲁木齐赛区) C. Coconut
    2017 ACM-ICPC 亚洲区(乌鲁木齐赛区) A. Banana
    DOCTYPE的作用?
    数组去重的方法
    闭包的分析?
    SVN(集中式管理)分析
  • 原文地址:https://www.cnblogs.com/mayouyou/p/13198905.html
Copyright © 2011-2022 走看看