Mysql----mysql练习题(单表多表查询)

创建数据库

 创建表(employee,department,salary)

Mysql----mysql练习题(单表多表查询)_第1张图片

 

Mysql----mysql练习题(单表多表查询)_第2张图片 

往表中插入数据 

Mysql----mysql练习题(单表多表查询)_第3张图片

 

 

 

练习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的雇员地址和电话;

select name,addr,tel from employee where num=001;

5. 查询表Employee表中女雇员的地址和电话,使用AS子句将结果列中各列的标题分别指定为地址、电话;

mysql> select name,addr as 地址,tel as 电话 from employee where sex='女';

6. 计算每个雇员的实际收入;

mysql> select num,inCome-outCome as 实际收入 from salay;

7. 找出所有姓王的雇员的部门号(部门号不能重复显示);

 

mysql> select distinct depno,name from employee where name like '王%';

8. 找出所有收入在2000-3000元之间的雇员编号

mysql> select num from salay where (inCome-outCome)>2000 and (inCome-outCome)<3000;

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


1. 查找在财务部工作的雇员情况;

select * from employee where depno=(select depno from department where depName='财务部');

2. 查找在财务部且年龄不低于研发部任一个雇员年龄的雇员的姓名;

select name from employee where depno in( select depno from department where depName='财务部' and birth < all( select birthh from employee where depno in( select depno from department where depName='研发部')));


3. 查找比所有财务部雇员收入都高的雇员的姓名;

SELECT name FROM employee WHERE depno IN(
SELECT depno FROM salay WHERE inCome>ALL(
SELECT inCome FROM salay WHERE depno IN(
SELECT depno FROM employee WHERE depno=(
SELECT depno FROM department WHERE depName='财务部'))));


练习3:连接查询的使用


1. 查找每个雇员的情况及薪水情况;

SELECT e.*,s.* FROM employee e
INNER JOIN salay s
ON e.num=s.num;

2. 查找财务部收入在2200元以上的雇员姓名及其薪水详细情况;

SELECT e.name, s.* FROM salay s
INNER JOIN employee e
ON s.Num =e.Num
INNER JOIN department d
ON e.depno=d.depno
WHERE s.income>2200 AND d.depname='财务部';


练习4:数据汇总


1. 求财务部雇员的平均实际收入;

select avg(inCome-outCome) as 财务部平均实际工资 from employee e inner join salay s on s.num=e.num inner join department d on d.depno=e.depno where d.depName='财务部'group by e.depno;

2. 求财务部雇员的总人数;

mysql> select sum(e.depno) as 财务部总人数 from employee e inner join salay s on s.num=e.num inner join department d on d.depno=e.depnoo where d.depName='财务部'group by e.depno;


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


1. 求各部门的雇员数(要求显示,部门号、部门名称和部门雇员数);

mysql> select d.depno,d.depName,sum(e.depno) from employee e inner join department d on d.depno=e.depno group by depno;

2. 求部门的平均薪水大于2500的部门信息(要求显示,部门号、部门名称和平均工资)
 

select d.depno,d.depname,avg(s.income) from employee e inner join department d on d.depno=e.depno inner join salay s on s.num=e.num group by e.depno having avg(s.inCome)>2500;

你可能感兴趣的:(mysql,数据库,sql)