Chapter 8 子查询

select job
from emp
where ename='SMITH'

select empno,ename,sal,job
from emp
where job ='CLERK'

select empno,ename,sal,job
from emp
where job=(select job from emp where ename='SMITH')

select empno,ename,sal,job
from emp
where job=(select job from emp where ename='SMITH') and sal<= 1100

SELECT empno,ename,sal,job
from emp
where sal<(select avg(sal)from emp)

select empno,ename,sal,job
from emp
where sal<(select avg(sal) from emp group by job);
--  返回错误:单行勃勃生机加多于一行

select empno,ename,sal,job
 from emp
 where ename=(select ename from emp where ename='ADAM')
 -- 返回未选定行R蛭猻elect语句返回空值。
 
  select job,min(sal),avg(sal),max(sal)
  from emp
  where job not like'PRESID%'
  group by job
  having avg(sal)>
                  (select min(avg(sal))
                    from emp
                    group by job)
 
 
 select e.empno,e.ename,e.sal,e.job,a.avesal
 fromemp e ,(select job,avg(sal) avesal from emp group by job) a
 where e.job=a.job
 and e.sal>a.avesal
 and e.job!='CLERK'
 
 
 select empno,ename,job,sal
 from emp
 where sal in(select max(sal) from emp group by job)
 and job <>'CLERK'
 and job not like'PRES%'
 
 
 select max(sal)
 from emp
 group by job
 
 
 
 select empno,ename,job,sal
 from emp
 where sal in(3000,1300,2975,5000,1600)
 and job<>'CLERK'
 and job not like 'PRES%'
 
 select empno,ename,job,sal
 from emp
 where sal in(select max(sal) from emp where job<>'CLERK'  and job not like'PRES%' group by job);

select e.empno, e.ename, e.sal, e.job
from emp e
where e.sal < ALL(
                  select avg(sal)
                  from emp
                  group by job
                  )
                  
<ALL 小于最小的
>ALL 大于最大的

select e.empno, e.ename, e.sal, e.job
from emp e
where e.sal = ALL(
                  select avg(sal)
                  from emp
                  group by job
                  )
                  
                  
select e.empno, e.ename, e.sal, e.job
from emp e
where e.sal < ANY(
                  select avg(sal)
                  from emp
                  group by job
                  )
                  
select e.empno, e.ename, e.sal, e.job
from emp e
where e.sal > ANY(
                  select avg(sal)
                  from emp
                  group by job
                  )
                  
                  
< ANY 小于最大的
> ANY 大于最小的
= ANY 相当于In

select e.empno, e.ename, e.sal, e.job
from emp e
where e.sal = ANY(
                  select avg(sal)
                  from emp
                  group by job
                  )
                  
select e.empno, e.ename, e.sal, e.job
from emp e
where e.sal in (
                  select avg(sal)
                  from emp
                  group by job
                  )
                  
                  
子查询中的空值(NULL)问题
select e.empno, e.ename, e.sal, e.job
from emp e
where e.mgr in (
               select w.mgr
               from emp w
               where w.mgr is null
               )

select e.empno, e.ename, e.sal, e.job
from emp e
where e.mgr not in (
               select w.mgr
               from emp w
               where w.mgr is null
               )
               
select e.empno, e.ename, e.sal, e.job
from emp e
where e.mgr not in (
               select w.mgr
               from emp w
               where w.mgr is not null
               )
               
select e.empno, e.ename, e.sal, e.job
from emp e
where e.mgr in (
               select w.mgr
               from emp w
               where w.mgr is not null
               )
               
update manager
set sal = 1300
where empno = 7521;

update manager
set sal = 1600
where empno = 7782;

commit;

select empno, ename, sal, job
from manager
where (sal, job) in (
            select max(sal), job
            from manager
            group by job
            );

select empno, ename, sal, job
from manager
where sal in (
            select max(sal)
            from manager
            group by job
            )
and  job in (
            select distinct job
            from manager
            group by job
            );

转载于:https://www.cnblogs.com/baoguo/articles/1554774.html

Chapter 8 子查询相关推荐

  1. MySQL 学习笔记(16)— 子查询(单行单列、一行多列、多行多列、 ALL、ANY、SOME 运算符、EXISTS 操作符)

    1. 子查询概念 子查询是指嵌套在其他语句(SELECT . INSERT . UPDATE . DELETE 等)中的 SELECT 语句:子查询也称为内查询( inner query )或者嵌套查 ...

  2. MySQL 学习笔记(4)— 组合查询、子查询、插入数据、更新/删除表数据、增加/删除表中的列以及重命名表

    1. 组合查询 1.表的加减法 表的加法,即求 product 和 product2 的并集,UNION 运算会除去重复的记录 SELECT product_id, product_name FROM ...

  3. mysql左加入_MySQL左加入子查询*

    我在JOIN语句中使用子查询组合了一个相当简单的查询.它仅在我在子查询select中包含*时才有效.为什么? 这很有效 $sql = 'SELECT locations.id, title, name ...

  4. Oracle 查询转换之子查询展开

    概念:子查询展开(Subquery Unnesting)是优化器处理带子查询的目标sql的一种优化手段,它是指优化器不再将目标sql中子查询当作一个独立的处理单元来单独执行,而是将该子查询转换为它自身 ...

  5. 浅谈 MySQL 子查询及其优化

    2019独角兽企业重金招聘Python工程师标准>>> 使用过oracle或者其他关系数据库的DBA或者开发人员都有这样的经验,在子查询上都认为数据库已经做过优化,能够很好的选择驱动 ...

  6. SQL Server 2005系列教学(6) 多表操作及子查询

    多表查询:<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> 人事 ...

  7. sql子查询示例_SQL更新查询示例说明

    sql子查询示例 In this article, we're going to learn how to use the SQL update statement - what it is, wha ...

  8. 数据库之子查询四(多重,表复制)

    一.多重子查询 select teaID,teaName,age,sex,dept,profession from tteacher where dept= (select dept  from te ...

  9. MySQL数据库子查询

    1. 子查询的介绍 在一个 select 语句中,嵌入了另外一个 select 语句, 那么被嵌入的 select 语句称之为子查询语句,外部那个select语句则称为主查询. 主查询和子查询的关系: ...

最新文章

  1. linux查看网卡速度
  2. 从头开始学eShopOnContainers——Visual Studio 2017环境配置
  3. DevExpress控件XtraGrid的Master-Detail用法 z
  4. SqlDictionary表,使用SQL语句查询TableID
  5. 115.不同的子序列
  6. Linux下重启tomcat
  7. 软件调试方法及调试原则
  8. 数据分析试题集+答案
  9. Mysql中的straight_join
  10. 重型柴油车OBD系统进入逻辑
  11. dd软件linux,dd工具
  12. Y z推荐菜东家 易订货生鲜系统_生鲜配送订货系统值得信赖
  13. 美国在PC处理器市场的垄断被ARM打破,国产处理器也取得了突破
  14. mysql 聚簇索引 和聚簇索引 (二级索引)的 那些事
  15. Linux基础系列(2命令帮助的详细获取)
  16. 每天5分钟玩转Kubernetes | Cluster IP底层实现
  17. 谈一谈SaaS产品的架构设计
  18. 讲解电脑重装系统后硬盘消失要如何解决
  19. 黑客自助餐,“生鱼片”盛宴
  20. Failed to invoke the method sayHello in the service com.itheima.service.HelloService.

热门文章

  1. Java毕业设计实战之book小说阅读系统的实现
  2. mysql中出现的Data truncated for column(表字段长度已经调整到足够长度)
  3. 智慧警务指挥决策平台
  4. C#基础--Lambda和LINQ
  5. HIT-软件构造blog5:博弈树
  6. spring应用手册-IOC(XML配置实现)-(23)-replaced-method标签
  7. iOS 通过URL地址来安装应用
  8. 可视化有关JS官网链接
  9. PS如何保存ICO文件
  10. EI——Towards Environment Independent Device Free Human Activity Recognition