MySQL练习题(单表多表查询)

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介: MySQL练习题(单表多表查询)

mysql练习题(单表多表查询

表1 Employee表
create table Employee(
  num int primary key auto_increment,
  name varchar(30) not null,
  addr varchar(30) not null unique,
  zip varchar(30) not null,
  tel varchar(30) not null,
  email varchar(30) unique,
  depno int not null,
  birth date not null,
  sex set ("男","女")
);
表2 Department
create table department(
  depno int primary key auto_increment,
  depName varchar(30) unique not null,
  remark varchar(50)
);
表3 salay
create table salay(
  num int primary key auto_increment,
  inCome double not null,
  outCome double not null
);
alter table Employee add constraint fk_Employee_department foreign key(depno) references department(depno);
往表里添加数据:
insert into Employee VALUES 
(1,"王林","武汉大学","430074","87598405",null,2,"1985-2-1","男"),
(2,"王芳 ","华中科大","430073","62534231",null,1,"1966-3-28","男"),
(3,"张晓","武汉理工大","430072 ","87596985",Null,1,"1972-12-9","男"),
(4,"王小燕","武汉交大","430071","85743261","lili@sina.com",1 ,"1950-7-30","女"),
(5,"李华"," 华中农大","430070","87569865",Null,5,"1962-10-18","男"),
(6,"李明","华中师大","430075","85362143","zhujun@sina.com ",5,"1955-09-28","男"),
(7,"田丽","中南财大","430076","85693265","zgming@sohu.com",3,"1968-08-10","女"),
(8,"吴天","武汉电力","430077","36985612 ","zjamg@china.com",5,"1964-10-01","男"),
(9,"刘备"," 武汉邮科院","430078","69865231",Null,3,"1967-04-02","男"),
(10,"赵云","学府家园","430071","68592312 ",Null,4,"1968-11-18","男"),
(11,"貂禅","湖北工大"," 430074","65987654", null,4,"1959-09-03","女");
insert INTO department(depName,remark) values ("财务部",Null),("人力资源部",Null),("经理办公室",Null),
("研发部",Null),("市场部",Null);
INSERT INTO salay(inCome,outCome) values (2100.7,123.09),(1582.62,88.03),(2569.88,185.65),(1987.01 ,79.58),
(2066.15 ,108.0),( 2980.7, 210.2),(3259.98 ,281.52),(2860.0,198),(2347.68,180),(2531.98,199.08),(2240.0,121.0);

练习1:SELECT语句的基本使用

  1. 查询每个雇员的所有记录;
    mysql> SELECT * FROM Employee;
  2. 查询前5个会员的所有记录;
    mysql> select * from Employee limit 5;
  3. 查询每个雇员的地址和电话;
    mysql> select name,addr,tel from Employee;
  4. 查询num为001的雇员地址和电话;
    mysql> select name,addr,tel from Employee where num=1;
  5. 查询表Employee表中女雇员的地址和电话,使用AS子句将结果列中各列的标题分别指定为地址、电话;
    mysql> select addr as “地址”,tel as "电话"from Employee where sex=“女”;
  6. 计算每个雇员的实际收入;
    mysql> select inCome-outCome as sal from salay;
  7. 找出所有性王的雇员的部门号(部门号不能重复显示);
    mysql> select distinct depno from Employee where name like “%王%”;
  8. 找出所有收入在2000-3000元之间的雇员编号
    mysql> select num from salay where inCome between 2000 and 3000;

练习2:子查询的使用(答案可以不唯一)

  1. 查找在财务部工作的雇员情况;
    mysql> select * from Employee where depno=(select depno from department where depName=“财务部”);
  2. 查找在财务部且年龄不低于研发部任一个雇员年龄的雇员的姓名;
    mysql> select name from Employee where depno=( select depno from department where depName=“财务部”) and birtth < all(select birth from Employee where depno=(select depno from department where depName=“研发部”));
  3. 查找比所有财务部雇员收入都高的雇员的姓名;
    mysql> select distinct name,income from Employee,salay,department where Employee.num=salay.num and income>all( select income from Employee,salay,department where Employee.num=salay.num and Employee.depno=department.depno and depName=“财务部”);

练习3:连接查询的使用

  1. 查找每个雇员的情况及薪水情况;
    mysql> select e.,s. from Employee e inner join salay s on e.num=s.num;
    mysql> select e.,s. from Employee e,salay s where e.num=s.num;
    mysql> select * from Employee,salay where Employee.num=salay.num;
  2. 查找财务部收入在2200元以上的雇员姓名及其薪水详细情况;
    mysql> select e.name,s.* from Employee e,salay s,department d where e.num=s.num and e.depno=d.depno
    -> and s.income>2000 and d.depname=“财务部”;

练习4:数据汇总

  1. 求财务部雇员的平均实际收入;
    mysql> select avg(s.inCome-s.outCome) from Employee e,salay s,department d where e.num=s.num and e.depno=d.depno and d.depname=“财务部”;
  2. 求财务部雇员的总人数;
    mysql> select count(*) from Employee e,salay s,department d where e.num=s.num and e.depno=d.depno and d.depnaame=“财务部”;

练习5:GROUP BY 、ORDER BY 子句的使用

  1. 求各部门的雇员数(要求显示,部门号、部门名称和部门雇员数);
    mysql> select e.depno “部门号”,d.depname “部门名称”,count(*) “部门雇员数” from Employee e,salay s,department d where e.num=s.num and e.depno=d.depno group by d.depno;
  2. 求部门的平均薪水大于2500的部门信息(要求显示,部门号、部门名称和平均工资)
    mysql> select e.depno “部门号”,d.depname “部门名称”,avg(s.inCome-s.outCome) “平均工资” from Employee e,salay s,department d where e.num=s.num and e.depno=d.depno group by d.depno having avg(s.inCome-s.outCome)>2500;
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
3月前
|
SQL 关系型数据库 MySQL
MySQL子查询篇(精选20道子查询练习题)-2
MySQL子查询篇(精选20道子查询练习题)
23 0
|
3月前
|
SQL 关系型数据库 MySQL
MySQL子查询篇(精选20道子查询练习题)-1
MySQL子查询篇(精选20道子查询练习题)
37 0
|
3月前
|
SQL 关系型数据库 MySQL
MySQL多表查询 子查询效率(DQL语句)
MySQL多表查询 子查询效率(DQL语句)
39 0
|
4月前
|
SQL 关系型数据库 MySQL
⑧【MySQL】数据库查询:内连接、外连接、自连接、子查询、多表查询
⑧【MySQL】数据库查询:内连接、外连接、自连接、子查询、多表查询
89 0
|
4月前
|
关系型数据库 MySQL Java
MySQL单表膨胀优化之MyCat分库分表
MySQL单表膨胀优化之MyCat分库分表
69 0
|
13天前
|
SQL 关系型数据库 vr&ar
多表查询——“MySQL数据库”
多表查询——“MySQL数据库”
|
21天前
|
SQL 关系型数据库 MySQL
mysql多表查询、函数查询
mysql多表查询、函数查询
|
1月前
|
SQL 关系型数据库 MySQL
Mysql多表查询详解
Mysql多表查询详解
25 0
|
1月前
|
关系型数据库 MySQL 数据库
【MySQL 数据库】3、多表查询
【MySQL 数据库】3、多表查询
23 0
|
3月前
|
SQL 关系型数据库 MySQL
MySQL 数值函数,字符串函数与多表查询
MySQL 数值函数,字符串函数与多表查询
15 0