mysql 练习题

  • 准备工作

    • 创建表格

      • 部门表 department

        • 字段

          • id:序号,自增,整型,主键
          • number:部门编号:varchar(15), 非空
          • name:部门名称,varchar(50),非空
          • location:部门所在地,varchar(50),非空
          • super_NO:部门经理 ID,varchar(15)
        • 创建表命令

          create table department(
              id int auto_increment primary key,
              number varchar(15) not null,
              name varchar(50) not null,
              location varchar(50) not null,
              super_NO varchar(15)
          );
          
        • 数据

          insert into department
              values 
                  (1,'D2019050001','清华集团','北京',null),
                  (2,'D2019050002','集团总部','北京','D2019050001'),
                  (3,'D2019060001','成都中心','成都','D2019050001'),
                  (4,'D2019060002','武汉中心','武汉','D2019050001'),
                  (5,'D2019090001','上海中心','上海','D2019050001'),
                  (6,'D2020010001','广州中心','广州','D2019050001'),
                  (7,'D2019090011','财务部','上海','D2019090001'),
                  (8,'D2020020012','行政部','上海','D2019090001'),
                  (9,'D2019060011','财务部','成都','D2019060001'),
                  (10,'D2019060012','技术部','成都','D2019060001'),
                  (11,'D2019060013','网络部','成都','D2019060001'),
                  (12,'D2019060014','市场部','成都','D2019060001');
          
      • 员工表 employee

        • 字段

          • id:编号,整型,自增,主键
          • number:员工编号,varchar(15),not null
          • name:员工姓名,varchar(50),not null
          • job:职务,varchar(50),not null
          • leader_NO:varchar(15)
          • hire_date:入职日期 date
          • salary:工资,decimal(10,2)
          • bonus:奖金,decimal(10,2)
          • level:等级,int
          • department_NO:varchar(15),not null
        • 命令

          create table employee(
              id int auto_increment primary key,
              number varchar(15) not null,
              name varchar(50) not null,
              job varchar(50) not null,
              leader_NO varchar(15),
              hire_date date not null,
              salary decimal(10,2),
              bonus decimal(10,2),
              level int,
              department_NO varchar(15)
          
          );
          
        • 数据

          insert into employee
              values
                  (1,'E2018010001','吴所为','总经理',null,'2018-01-01',2800.00,4000.00,9,'D2019060001'),
                  (2,'E2018070003','韩金龙','总经理',null,'2018-07-01',2800.00,4000.00,8,'D2019090001'),
                  (3,'E2018060002','王黎明','总经理',null,'2018-06-01',2800.00,4000.00,8,'D2019060002'),
                  (4,'E2018020002','龚爱国','总经理',null,'2018-02-01',2800.00,4000.00,8,'D2020010001'),
                  (5,'E2019050001','马金花','财务总监','E2018010001','2019-01-01',3800.00,500.00,6,'D2019060011'),
                  (6,'E2019050018','李昌贵','财务专员','E2019050001','2019-04-21',2800.00,800.00,4,'D2019060011'),
                  (7,'E2019100011','王建国','网络管理员','E2018010001','2019-10-01',3200.00,null,5,'D2019060013'),
                  (8,'E2019110004','黎锦熙','网络管理员','E2019100011','2019-11-01',3200.00,null,5,'D2019060013'),
                  (9,'E2020020023','繁茂森','销售专员','E2019060005','2020-02-01',2800.00,0.00,4,'D2019060014'),
                  (10,'E2019060005','张善民','销售经理','E2018010001','2019-06-01',2800.00,500.00,6,'D2019060014'),
                  (11,'E2019060009','廖云龙','技术总监','E2018010001','2019-06-01',4800.00,2000.00,7,'D2019060012'),
                  (12,'E2019120021','刘盛会','研发工程师','E2019060009','2019-12-11',4800.00,500.00,5,'D2019060012'),
                  (13,'E2019020001','马明全','高级工程师','E2019060009','2019-02-01',4800.00,1000.00,6,'D2019060012'),
                  (14,'E2019120015','李意','行政专员','E2018070003','2019-12-20',2800.00,500.00,4,'D2019090001'),
                  (15,'E2019020017','刘六一','财务总监','E2018070003','2019-02-16',3800.00,1000.00,6,'D2019090011'),
                  (16,'E2020020012','陈超','研发工程师','E2019060009','2020-02-18',4200.00,500.00,5,'D2019060012');
          
  • 习题

    • 单表练习

        1. 查询出部门编号为 D2019060011 的所有员工
        select * 
            from employee
            where department_NO='D2019060011';
        
        • 验证
          image
        1. 所有财务总监的姓名、员工编号和部门编号。
        select name,number,department_NO
            from employee
            where job='财务总监';
        
        • 验证
          image
        1. 找出奖金高于工资的员工。
        select name 
            from employee
            where bonus>salary;
        
        • 验证
          image
        1. 找出奖金高于工资的 40% 的员工。
        select name
            from employee
            where bonus>salary*0.4;
        
        • 验证
          image
      • 5 找出部门编号为 D2019090011 中所有财务总监,和部门编号为 D2019060011 中所有财务专员的详细资料。

        select *
            from employee
            where department_NO='D2019090011' and job='财务总监' 
            or department_NO='D2019060011' and job='财务专员';
        
        • 验证
          image
        1. 找出部门编号为 D2019090001 中所有总经理,部门编号为 D2019090011 中所有财务总监,还有即不是总经理又不是销售总监但其工资大或等于 4000 的所有员工详细资料。
        select * 
            from employee
            where (department_NO='D2019090001' and job='总经理' )
            or (department_NO='D2019090011' and job='财务总监')
            or(job!='总经理' and job!='销售总监' and salary>=4000);
        
        • 验证
          image
      • 7.有奖金的工种。

        select job
            from employee
            where bonus is not null
            group by job;
        
      • 8.无奖金或奖金低于 1000 的员工。

        select name,bonus
            from employee
            where bonus is null or bonus<1000;
        
        1. 查询名字由两个字组成的员工。
        • 汉字通常中 3 个字节
        select name
            from employee
            where length(name)=6;
        
         select name
            from employee
            where name like '__';
        
      • 10.查询 2020 年入职的员工。

        select name
            from employee
            where year(hire_date)='2020';
        
        select name
            from employee
            where hire_date like '2020%';
        
        1. 查询所有员工详细信息,用员工编号升序排序。
        select *
            from employee
            order by number;
        
        1. 查询所有员工详细信息,用工资降序排序,如果工资相同使用入职日期升序排序。
         select *
            from employee
            order by salary desc,hire_date asc;
        
        1. 查询每个部门的平均工资。

          select *
          	from employee
          	group by department_NO;
          
        1. 求出每个部门的雇员数量。
        • select department_NO,count(*) as 员工人数

          • from employee
          • group by department_NO;
        1. 查询每种工作的最高工资、最低工资、人数.
        • select job,max(salary) as 最高工资,min(salary) as 最低工资,count(id) as 人数

          • from employee
          • group by job;
        1. 列出最低薪金大于 4000 的各种工作及从事此工作的员工人数。
        • select job,count(id) as 员工人数

          • from employee
          • where salary>4000
          • group by job;
        1. 统计各部门工资总和,显示部门编号和该部门雇员的月工资的总和,并且要满足该部门雇员的月工资合计大于 6000,输出结果按月工资的合计升序排列。
        • select department_NO,sum(salary) as 工资总和

          • from employee
          • group by department_NO
          • having 工资总和>6000
          • order by 工资总和;
    • 多表练习

        1. 列出所有员工的姓名及其直接上级的姓名。
        • 这里两个字段都在一个表中,但是也要进行多表连接,自己连接自己

        • select e1.name as 员工名称,e2.name as 直接领导

          • from employee as e1 inner join employee as e2
          • on e1.leader_NO=e2.number;
        • select e1.name as 员工名称,e2.name as 直接领导

          • from employee as e1,employee as e2
          • where e1.leader_NO=e2.number;
        1. 列出受雇日期早于直接上级的所有员工的编号、姓名、部门名称。
        • 这题要分几部分完成

          • 1、先完成显示所有员工入职日期和他的直接领导的入职日期,先自己连接自己

            • select *

              • from employee as e1 inner join employee as e2
              • on e1.leader_NO =e2.number;
            • 这样即显示了自己的入职时间,也显示了直接领导的入职时间

          • 2、加入筛选条件

            • select e1.number as 员工编号,e1.name as 员工名称,e1.hire_date as 员工入职时间,e2.name as 直接领导名称,e2.hire_date as 直接领导入职时间

              • from employee as e1 inner join employee as e2
              • on e1.leader_NO =e2.number
              • where e1.hire_date<e2.hire_date;
          • 3、还要显示部门的名称,所以还要连接 department 表

            • select e1.number as 员工编号,e1.name as 员工名称,d.name as 部门名称

              • from employee as e1 inner join employee as e2
              • on e1.leader_NO =e2.number
              • inner join department as d
              • on e1.department_NO=d.number
              • where e1.hire_date<e2.hire_date;
        • select e1.number as 员工编号,e1.name as 姓名,d.name as 部门名称

          • from employee as e1 inner join employee as e2
          • on e1.leader_NO=e2.number
          • inner join department as d
          • on e1.department_NO=d.number
          • where e1.hire_date<e2.hire_date;
        1. 列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门。
        • select *

          • from department as d left outer join employee as e
          • on d.number=e.department_NO
        1. 列出在财务部工作的员工的姓名,假定不知道财务部的部门编号。
        • 注意子查询的用法

        • select name

          • from employee
          • where department_NO in(select number from department where name='财务部');
        1. 列出薪金高于公司平均薪金的所有员工信息,所在部门名称,上级领导。
        • 第一步、显示所有员工信息和以及部门领导的名字

          • select *

            • from employee as e1 inner join employee as e2
            • on e1.leader_NO=e2.number;
        • 第二步、显示出部门名称,因为部门名称在 department 表中,所以要连接它

          • select e1.number,e1.name,e1.salary,e1.department_NO,e1.leader_NO,e2.name,d.name

            • from employee as e1 inner join employee as e2
            • on e1.leader_NO=e2.number
            • inner join department as d
            • on e1.department_NO=d.number;
        • 第三步、筛选出大于平均工资的记录

          • select e1.number,e1.name,e1.salary,e1.department_NO,e1.leader_NO,e2.name,d.name

            • from employee as e1 inner join employee as e2
            • on e1.leader_NO=e2.number
            • inner join department as d
            • on e1.department_NO=d.number
            • where e1.salary>(select avg(salary) from employee);
        1. 列出与陈超从事相同工作的所有员工及部门名称。
        • select e.*,d.name

          • from employee as e inner join department as d
          • on e.department_NO=d.number
          • where e.job=(select job from employee where name='陈超');
        1. 查出至少有一个员工的部门。显示部门编号、部门名称、部门位置、部门人数。
        • select e.number as 部门编号,d.name as 部门名称 ,d.location as 部门位置,count(*) as 部门人数

          • from employee as e inner join department as d
          • on e.department_NO=d.number
          • group by d.number
          • having 部门人数>=1;
        1. 列出薪金高于在财务部工作员工平均薪金的员工姓名和薪金、部门名称
        • select e.name as 员工姓名,e.salary as 员工薪金,d.name as 部门名称

          • from employee as e inner join department as d
          • on e.department_NO=d.number
          • where salary>(select avg(salary) from employee where department_NO in(select number from department where name='财务部'));
  • MySQL

    MySQL 是一个关系型数据库管理系统,由瑞典 MySQL AB 公司开发,目前属于 Oracle 公司。MySQL 是最流行的关系型数据库管理系统之一。

    694 引用 • 537 回帖 • 1 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...

推荐标签 标签

  • 一些有用的避坑指南。

    69 引用 • 93 回帖 • 1 关注
  • Eclipse

    Eclipse 是一个开放源代码的、基于 Java 的可扩展开发平台。就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境。

    76 引用 • 258 回帖 • 624 关注
  • Outlook
    1 引用 • 5 回帖 • 2 关注
  • 创造

    你创造的作品可能会帮助到很多人,如果是开源项目的话就更赞了!

    186 引用 • 1021 回帖
  • Hprose

    Hprose 是一款先进的轻量级、跨语言、跨平台、无侵入式、高性能动态远程对象调用引擎库。它不仅简单易用,而且功能强大。你无需专门学习,只需看上几眼,就能用它轻松构建分布式应用系统。

    9 引用 • 17 回帖 • 642 关注
  • Google

    Google(Google Inc.,NASDAQ:GOOG)是一家美国上市公司(公有股份公司),于 1998 年 9 月 7 日以私有股份公司的形式创立,设计并管理一个互联网搜索引擎。Google 公司的总部称作“Googleplex”,它位于加利福尼亚山景城。Google 目前被公认为是全球规模最大的搜索引擎,它提供了简单易用的免费服务。不作恶(Don't be evil)是谷歌公司的一项非正式的公司口号。

    49 引用 • 192 回帖
  • Vim

    Vim 是类 UNIX 系统文本编辑器 Vi 的加强版本,加入了更多特性来帮助编辑源代码。Vim 的部分增强功能包括文件比较(vimdiff)、语法高亮、全面的帮助系统、本地脚本(Vimscript)和便于选择的可视化模式。

    29 引用 • 66 回帖
  • TGIF

    Thank God It's Friday! 感谢老天,总算到星期五啦!

    291 引用 • 4495 回帖 • 663 关注
  • Log4j

    Log4j 是 Apache 开源的一款使用广泛的 Java 日志组件。

    20 引用 • 18 回帖 • 36 关注
  • Tomcat

    Tomcat 最早是由 Sun Microsystems 开发的一个 Servlet 容器,在 1999 年被捐献给 ASF(Apache Software Foundation),隶属于 Jakarta 项目,现在已经独立为一个顶级项目。Tomcat 主要实现了 JavaEE 中的 Servlet、JSP 规范,同时也提供 HTTP 服务,是市场上非常流行的 Java Web 容器。

    162 引用 • 529 回帖 • 8 关注
  • WebSocket

    WebSocket 是 HTML5 中定义的一种新协议,它实现了浏览器与服务器之间的全双工通信(full-duplex)。

    48 引用 • 206 回帖 • 279 关注
  • 外包

    有空闲时间是接外包好呢还是学习好呢?

    26 引用 • 233 回帖 • 1 关注
  • PostgreSQL

    PostgreSQL 是一款功能强大的企业级数据库系统,在 BSD 开源许可证下发布。

    22 引用 • 22 回帖 • 3 关注
  • 导航

    各种网址链接、内容导航。

    45 引用 • 177 回帖 • 1 关注
  • Swagger

    Swagger 是一款非常流行的 API 开发工具,它遵循 OpenAPI Specification(这是一种通用的、和编程语言无关的 API 描述规范)。Swagger 贯穿整个 API 生命周期,如 API 的设计、编写文档、测试和部署。

    26 引用 • 35 回帖 • 4 关注
  • MyBatis

    MyBatis 本是 Apache 软件基金会 的一个开源项目 iBatis,2010 年这个项目由 Apache 软件基金会迁移到了 google code,并且改名为 MyBatis ,2013 年 11 月再次迁移到了 GitHub。

    173 引用 • 414 回帖 • 365 关注
  • Jenkins

    Jenkins 是一套开源的持续集成工具。它提供了非常丰富的插件,让构建、部署、自动化集成项目变得简单易用。

    54 引用 • 37 回帖 • 1 关注
  • React

    React 是 Facebook 开源的一个用于构建 UI 的 JavaScript 库。

    192 引用 • 291 回帖 • 369 关注
  • SOHO

    为成为自由职业者在家办公而努力吧!

    7 引用 • 55 回帖 • 2 关注
  • Markdown

    Markdown 是一种轻量级标记语言,用户可使用纯文本编辑器来排版文档,最终通过 Markdown 引擎将文档转换为所需格式(比如 HTML、PDF 等)。

    172 引用 • 1538 回帖
  • 阿里云

    阿里云是阿里巴巴集团旗下公司,是全球领先的云计算及人工智能科技公司。提供云服务器、云数据库、云安全等云计算服务,以及大数据、人工智能服务、精准定制基于场景的行业解决方案。

    85 引用 • 324 回帖
  • TextBundle

    TextBundle 文件格式旨在应用程序之间交换 Markdown 或 Fountain 之类的纯文本文件时,提供更无缝的用户体验。

    1 引用 • 2 回帖 • 82 关注
  • App

    App(应用程序,Application 的缩写)一般指手机软件。

    91 引用 • 384 回帖
  • PWA

    PWA(Progressive Web App)是 Google 在 2015 年提出、2016 年 6 月开始推广的项目。它结合了一系列现代 Web 技术,在网页应用中实现和原生应用相近的用户体验。

    14 引用 • 69 回帖 • 186 关注
  • Lute

    Lute 是一款结构化的 Markdown 引擎,支持 Go 和 JavaScript。

    29 引用 • 202 回帖 • 29 关注
  • Telegram

    Telegram 是一个非盈利性、基于云端的即时消息服务。它提供了支持各大操作系统平台的开源的客户端,也提供了很多强大的 APIs 给开发者创建自己的客户端和机器人。

    5 引用 • 35 回帖
  • Sublime

    Sublime Text 是一款可以用来写代码、写文章的文本编辑器。支持代码高亮、自动完成,还支持通过插件进行扩展。

    10 引用 • 5 回帖