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

    1、子查询
        是指嵌入在其他sql语句中的select语句,也叫嵌套语句
    2、单行子查询
        单行子查询是指只返回一行数据的子查询语句
        Q:如何显示与SMITH同一部门的所有员工
            select * from emp where deptno=(select deptno from emp where
            ename='SMITH');
    3、多行子查询
        多行子查询是指返回多行数据的子查询
        Q:如何查询和部门10的工作相同的雇员的名字、岗位、
            工资、部门号
            select * from emp where job in(select distinct job from emp where
            deptno=10);
    4、多行子查询中使用all操作符
        Q:如何显示工资比部门30的所有员工的工资高的员工的姓名、工资、
            部门号。
               ① select ename,sal,deptno from emp where 
                sal>all(select sal from emp where deptno=30 );
                ②select ename,sal,deptno from emp where 
                sal>(select max(sal) from emp where deptno=30);
    5、在多行子查询中使用any操作符
        Q:如何显示工资比部门30的任意一个员工的工资高的员工的姓名、工资和部门号
                ①select ename,sal,deptno from emp where sal>any
                (select sal from emp where deptno=30);
                ②select ename,sal,deptno from emp where 
                sal>(select min(sal) from emp where deptno=30);
     
        Q:如何显示与SMITH部门和岗位完全相同的所有员工
            select * from emp where (deptno,job)=(select deptno,job from emp 
             where ename='SMITH');
     
    6、在from子句中使用子查询
        Q:如何显示高于自己部门平均工资的员工的信息
            1、select deptno,avg(sal) myAvgSal from emp group by deptno;
            2、select  a1.ename,a1.deptno,a1.sal,a2.deptno,a2.myAvgSal from emp a1,(select deptno,avg(sal) myAvgSal from emp group by deptno) a2 where  a1.deptno=a2.deptno and a1.sal>a2.myAvgSal; 
  • 相关阅读:
    CodeForces gym Nasta Rabbara lct
    bzoj 4025 二分图 lct
    CodeForces 785E Anton and Permutation
    bzoj 3669 魔法森林
    模板汇总——快读 fread
    bzoj2049 Cave 洞穴勘测 lct
    bzoj 2002 弹飞绵羊 lct裸题
    HDU 6394 Tree 分块 || lct
    HDU 6364 Ringland
    nyoj221_Tree_subsequent_traversal
  • 原文地址:https://www.cnblogs.com/ZKeJun/p/8940064.html
Copyright © 2011-2022 走看看