文章目录
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语句的基本使用
-
查询每个雇员的所有记录;
mysql> SELECT * FROM Employee; -
查询前5个会员的所有记录;
mysql> select * from Employee limit 5; -
查询每个雇员的地址和电话;
mysql> select name,addr,tel from Employee; -
查询num为001的雇员地址和电话;
mysql> select name,addr,tel from Employee where num=1; -
查询表Employee表中女雇员的地址和电话,使用AS子句将结果列中各列的标题分别指定为地址、电话;
mysql> select addr as “地址”,tel as "电话"from Employee where sex=“女”; -
计算每个雇员的实际收入;
mysql> select inCome-outCome as sal from salay; -
找出所有性王的雇员的部门号(部门号不能重复显示);
mysql> select distinct depno from Employee where name like “%王%”; -
找出所有收入在2000-3000元之间的雇员编号
mysql> select num from salay where inCome between 2000 and 3000;
练习2:子查询的使用(答案可以不唯一)
-
查找在财务部工作的雇员情况;
mysql> select * from Employee where depno=(select depno from department where depName=“财务部”); -
查找在财务部且年龄不低于研发部任一个雇员年龄的雇员的姓名;
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=“研发部”)); -
查找比所有财务部雇员收入都高的雇员的姓名;
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:连接查询的使用
-
查找每个雇员的情况及薪水情况;
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; -
查找财务部收入在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:数据汇总
-
求财务部雇员的平均实际收入;
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=“财务部”; -
求财务部雇员的总人数;
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 子句的使用
-
求各部门的雇员数(要求显示,部门号、部门名称和部门雇员数);
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; -
求部门的平均薪水大于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;