mysql子查询复杂查询

课程名称数据库

MySQL数据库技术spa

实验ci

成绩table

 

实验名称select

实验五:复杂查询语法

学号方法

 辅导老师;陶荣技术

姓名总结

 

班级数据

 

日期

 

实验目的:

1.掌握select语句的基本语法;

2.掌握链接查询和子查询的使用方法;

实验平台:

MySQL+SQLyog;

实验内容与步骤:

如下操做均在YGGL数据库中进行。

1、子查询练习:

1. 查找在财务部工做的员工的状况。

 

2. 使用子查询的方法查找全部收入在2500元如下的员工的状况。

 

3. 查找研发部年龄不低于市场部全部员工年龄的雇员姓名。

 

4. 用子查询的方法查找研发部比市场部全部雇员收入都高的雇员的姓名。

 

5. 查找比广告部全部员工收入都高的员工的姓名。

 

6. 在order by中使用子查询,查询员工姓名、性别和工龄信息,要求按

实际收入从大到小排列。

 

2、链接查询练习:

1. 查询每一个员工的状况及其薪水的状况。

 

2. 使用内链接的方法查询名字为“王林”的员工所在的部门。

 

3. 使用内链接方法查找不在广告部工做的全部员工信息。

 

4. 使用外链接方法查找全部员工的月收入。

 

5. 查找广告部收入在2000元以上的雇员姓名及其薪水详情。

 

6. 查询研发部在1966年之前出生的员工姓名及其薪水状况。

 

 

实验总结(结论或问题分析):

      select * from `employees`  where 部门号 IN(select 部门号 FROM departments where 部门号=1);

 

select * from employees e,`salary`s

where e.编号=s.编号 and 收入<2500

 

select 姓名  from employees e,departments d

where 出生日期>=all(SELECT 出生日期 from employees e, departments d

where e.部门号=d.部门号 and d.部门名称='财务部')

and e.部门号=d.部门号 AND d.部门名称='研发部';

 

select 姓名  from employees e,departments d,salary s

where 收入>all(select 收入 FROM employees e,departments d, salary s

where e.部门号=d.部门号 and s.编号=e.编号 and d.部门名称='市场部')

 and e.部门号=d.部门号 and s.编号=e.编号 and d.部门名称='研发部';

 

 

SELECT 姓名 from `employees` e where 部门号 in(

SELECT 部门号 FROM salary s where 

 收入>=all (select 收入 from salary where 编号 

 IN(select 编号 from employees e where  部门号=(select 

 部门号 from departments where 部门名称='研发部'))));

 

 SELECT 姓名,性别 from employees e,`salary`s

 where s.编号=e.编号 ORDER BY 收入 desc;

 

select * from employees e,salary s 

where s.编号=e.编号;

 

select 部门名称 from employees e 

inner join departments d  on e.部门号=d.部门号

where e.姓名='王加';

 

select* from employees e 

inner join departments d  on e.部门号=d.部门号

where d.部门号!='广告部';

 

select * from employees e left outer join salary s 

on s.编号=e.编号;

 

 

select * from employees e inner join salary s 

on s.编号=e.编号 inner join departments d on  

e.部门号=d.部门号 where s.收入>2000;

 

 

select * from employees e inner join salary s 

on s.编号=e.编号 inner join departments d on  

e.部门号=d.部门号 where e.出生日期>'1966' and d.部门名称='研发部';