码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【MYSQL】复合查询


    文章目录

    • 基本查询
      • 查询工资高于500 或 岗位是MANGER的雇员,同时还要满足他们的姓名首字母为大写J
      • 按照部门号升序、工资降序排序
      • 使用年薪降序排序
      • 显示工资最高的员工的名字和工作岗位
      • 显示工资高于平均工资的员工信息
      • 显示每个部门的平均工资和最高工资
      • 显示平均工资低于2000的部门号和它的平均工资
      • 显示每种岗位的员工总数,以及平均工资
    • 多表查询
      • 显示雇员名、雇员工资、以及员工所在部门的名字
      • 显示各个员工的姓名、工资、工资级别
      • 自连接
      • 显示员工FORD的上级领导的编号和姓名
    • 子查询
      • 单行子查询
        • 显示与SMITH同一部门的员工
      • 多行子查询
        • 查询和10号部门的工作岗位相同的雇员的名字、岗位、工资、部门号,但不包含10号自己的
        • 显示工资比部门30号 所有员工的工资高的员工的姓名、工资、和部门号
      • 显示工资比30号部门的任意员工的工资高的员工的姓名工资和部门号
      • 多列子查询
        • 查询和SMITH部门和岗位完全相同的雇员,不含SMITH本人
      • 在from语句中使用子查询
        • 显示每个高于自己部门平均工资的员工的姓名、部门、工资、平均工资
        • 查找每个部门工资最高的人的姓名、工资、部门、最高工资
        • 显示每个部门的信息(部门名、编号、地址)和人员数量
      • 合并查询
        • union
        • 将工资大于2500 或 职位是MANAGER的人找出来
        • union all

    基本查询

    查询工资高于500 或 岗位是MANGER的雇员,同时还要满足他们的姓名首字母为大写J

    select ename from emp where (sal > 500 OR job = 'MANGER') and substring(ename,1,1) = 'J';    
    
    • 1

    按照部门号升序、工资降序排序

    select * from emp order by deptno asc,sal desc;
    
    • 1

    使用年薪降序排序

    select sal*12+ifnull(comm,0) as sum from emp order by sum desc;
    
    • 1

    显示工资最高的员工的名字和工作岗位

    select * from emp where sal = (select max(sal) from emp);
    
    • 1

    显示工资高于平均工资的员工信息

    select * from emp where sal > (select avg(sal) from emp);
    
    • 1

    显示每个部门的平均工资和最高工资

    select deptno,format(max(sal),2) 最高 , format(avg(sal),2) 平均 from emp group by deptno;
    
    • 1

    显示平均工资低于2000的部门号和它的平均工资

    select deptno,avg(sal) 平均 from emp group by deptno having 平均 > 2000;
    
    • 1

    显示每种岗位的员工总数,以及平均工资

    select job,count(*) 人数,avg(sal) 平均工资 from emp group by job;
    
    • 1

    多表查询

    显示雇员名、雇员工资、以及员工所在部门的名字

    两张表进行笛卡尔积。即用第一张表的一条数据和第二张表的所有数据进行组合。

    select emp.ename,sal,dname from emp,dept where emp.deptno=dept.deptno;
    
    • 1

    显示各个员工的姓名、工资、工资级别

    select ename,sal,grade from emp,salgrade where sal between losal and hisal; 
    
    • 1

    自连接

    select * from salgrade as t1,salgrade as t2;
    
    • 1

    显示员工FORD的上级领导的编号和姓名

    //子查询
    select mgr from emp where ename = 'FORD';
    
    select ename,emptno from emp where empno = (select mgr from emp where ename = 'FORD');
    
    //自连接
    select * from emp e1,emp e2 where e1.name='FORD' and e1.mgr=e2.empno;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    子查询

    嵌入在其他sql语句中的select语句,也叫嵌套查询

    单行子查询

    显示与SMITH同一部门的员工

    select dptno from emp where ename = 'SMITH';
    
    select * from emp where deptno=(select dptno from emp where ename = 'SMITH')
    
    
    • 1
    • 2
    • 3
    • 4

    多行子查询

    查询和10号部门的工作岗位相同的雇员的名字、岗位、工资、部门号,但不包含10号自己的

    select ename,job,sal,deptno from emp where job in   (select distinct job from emp where deptno=10) and deptno <> 10;
    
    • 1

    还要员工的部门名字:

    select * from (select ename,job,sal,deptno from emp where job in (select distinct job from emp where deptno=10) and deptno <> 10) as tmp , dept where dept.deptno=tmp.deptno;
    
    • 1

    显示工资比部门30号 所有员工的工资高的员工的姓名、工资、和部门号

    select * from emp where sal > (select max(sal) from emp where deptno=30);
    
    select * from emp where sal > all(select distinct sal from emp where deptno=30);
    
    • 1
    • 2
    • 3

    显示工资比30号部门的任意员工的工资高的员工的姓名工资和部门号

    select * from emp where sal > any(select distinct sal from emp where deptno=30);
    
    • 1

    多列子查询

    查询和SMITH部门和岗位完全相同的雇员,不含SMITH本人

    select * from emp where (dept,job) in (select deptno,job from emp where ename='SMITH') and ename <> 'SMITH';
    
    • 1

    在from语句中使用子查询

    显示每个高于自己部门平均工资的员工的姓名、部门、工资、平均工资

    //1.每个部门的平均工资
    select deptno,avg(sal) from emp group by deptno;
    
    //2.进行笛卡尔积
    select * from emp,(select deptno,avg(sal) from emp group by deptno) as tmp;
    
    //3.筛选正确的部门
    select * from emp,(select deptno,avg(sal) from emp group by deptno) as tmp where emp.deptno = tmp.deptno;
    
    //4.单表查询
    select * from emp,(select deptno,avg(sal) myavg from emp group by deptno) as tmp where emp.deptno = tmp.deptno and emp.sal > tmp.myavg;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    查看这一些人的办公地点:

    1.上面的查询结果和dept表进行笛卡尔积
    2.筛选合适的行
    3.按条件得到要求的
    
    • 1
    • 2
    • 3

    查找每个部门工资最高的人的姓名、工资、部门、最高工资

    //1.每个部门的最高工资
    select deptno,max(sal) from emp group by deptno;
    
    //2.和emp表笛卡尔积,找到谁是最高的那个人
    select ename,sal,t1.deptno,mymax from emp t1, (select deptno,max(sal) mymax from emp group by deptno) t2 where t1.deptno = t2.deptno and t1.sal = t2.mymax;
    
    //3.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    显示每个部门的信息(部门名、编号、地址)和人员数量

    • 使用子查询
    //各部门人员数量
    select deptno,count(*) dept_num from emp group by deptno;
    
    //跟dept表进行笛卡尔积
    select t1.dname,t1.loc,t2.dept_num,t1.deptno from dept t1,(select deptno,count(*) dept_num from emp group by deptno) t2 where t1.deptno=t2.deptno;
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    多表查询的本质:将多表转换成单表。

    合并查询

    union

    该操作符用于取得两个结果集的并集,当使用该操作符时,会自动去掉结果集中的重复行(自动去重)。要拼接的时候要满足两个列一致,否则不能拼。

    将工资大于2500 或 职位是MANAGER的人找出来

    select * from emp where job = 'MANNAGER' union select * from emp where job = 'MANAGER';
    
    • 1

    union all

    该操作符不会对满足多个条件的数据进行去重,而是保留。

  • 相关阅读:
    分布式文件系统HDFS-2
    Java | Leetcode Java题解之第122题买卖股票的最佳时机II
    zookeeper mac安装
    自己动手从零写桌面操作系统GrapeOS系列教程——13.向MBR中写入程序
    中国传统节日春节网页HTML代码 春节大学生网页设计制作成品下载 学生网页课程设计期末作业下载 DW春节节日网页作业代码下载
    MYSQL(事务+锁+MVCC+SQL执行流程)理解(2)
    激光SLAM多层料箱机器人HEGERLS A42M SLAM|灵活匹配多种作业高度|支持多尺寸纸箱/料箱混合拣选
    webpack + vite 前端构建工具
    java计算机毕业设计流浪狗领养系统源码+mysql数据库+系统+lw文档+部署
    【微服务架构】基础的微服务架构模板、fianceCampus项目
  • 原文地址:https://blog.csdn.net/m0_73209194/article/details/133652693
  • 最新文章
  • 【JVM】编译执行与解释执行的区别是什么?JVM 使用哪种方式?
    用 Hashids 优雅解决 C 端自增 ID 暴露问题
    V8引擎 精品漫游指南--Ignition篇(上) 指令 栈帧 槽位 调用约定 内存布局 基础内容
    LLVM Pass快速入门(四):代码插桩
    milkup:桌面端 markdown AI续写和即时渲染
    基于项目工程构建SBOM(软件物料清单)的研究
    鸿蒙应用开发UI基础第二节:鸿蒙应用程序框架核心解析与实操
    .NET 中如何快速实现 List 集合去重?
    扣子Coze实战:从0到1打造抖音+小红书热点监控智能体
    浅谈数据访问层
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号