MySQL笔记及习题(六)--DML和子查询习题

6.增删改(dml)
/*联合查询
union 联合 合并:将多条查询语句合并成一个结果
语法:查询语句1 union 查询语句2 union......

应用场景:要求查询的结果来自于多个表,并且多个表没有直接的联系关系,但查询的信息是一致的

特点:
	1.要求多条查询语句的查询列数是一致的
	2.要求多条查询语句的查询的每一列的类型和顺序最好一致
	3.union关键字默认去重,如果使用union all 可以包含重复项
*/
--1.查询部门编号>90或邮箱包含a的员工信息
SELECT * FROM employees WHERE email LIKE '%a%' OR department_id>90;;

SELECT * FROM employees  WHERE email LIKE '%a%'
UNION
SELECT * FROM employees  WHERE department_id>90;

--2.查询中国用户中男性的信息以及外国用户中年男性的用户信息
SELECT id,cname FROM t_ca WHERE csex='男'
UNION ALL
SELECT t_id,tname FROM t_ua WHERE tGender='male';


*********DML数据的增删改********
	插入:insert
	删除:delete
	修改:update
insert语法:
	1.insert into 表名(字段名1,字段名2,...) values(字段名1,字段名2)
	2.insert into 表名 values (全部字段)
	插入的类型必须要与列的类型一致或者是兼容
	insert into beauty(id,`name`,sex) values(13,'唐艺昕','女');
update语法:
	update 表名 set(字段)=新值,... where 条件
	#修改女神表中唐姓女生的电话为12345678988(单表修改)
	update beauty set phone='12345678988' where `name` like '唐%';
	#修改张无忌的女朋友的手机号为115
	update beauty b join boys on b.boyfriend_id=boys.id set b.phone='115' where boys.boyName='张无忌';
delete语法:
	delete from 表名 where 筛选条件
	#删除女神表id为2的数据(单表删除)
	delete from beauty where id=2;
	#删除张无忌女朋友的信息(多表删除)
	delete b from beauty b join boys on b.boyfriend_id=boys.id where boys.boyName='张无忌';
	
	truncate
	deletetruncate比较:
	1.delete后面可以跟where过滤条件,而truncate不行
	2.truncate删除,效率会比delete高那么一丢丢
	3.truncate删除是没有返回值的,而delete4.truncate删除是不能回滚的,而delete
  • 子查询习题
**********习题**********
1. 查询工资最低的员工信息: last_name, salary
select last_name,salary from employees where salary=( select min(salary) from employees );

2. 查询平均工资最低的部门信息
#(1.)
select *, from departments where department_id=(
select department_id from (select avg(salary) pj,department_id from employees GROUP BY department_id) bpj where bpj.pj=
(select min(a.pj) from (select avg(salary) pj,department_id from employees GROUP BY department_id) a,departments )
);
#(2.)
select * from departments 
where department_id = (
	select department_id from employees GROUP BY department_id HAVING avg(salary) = (
		select min(e.avg) from (
			select avg(salary) avg from employees GROUP BY department_id
		) e
	)
);
#思路(查询平均工资最低的部门信息)
#先查出每个部门的平均工资
select avg(salary) from employees GROUP BY department_id
#再从里面找出最低的工资
select min(di.ag) from (
	select avg(salary) ag from employees GROUP BY department_id
) di
#求这是那个部门的最低工资(部门号)
select department_id from employees GROUP BY department_id HAVING avg(salary)=(
	select min(di.ag) from (
		select avg(salary) ag from employees GROUP BY department_id
	) di
)
#最后求出平均工资最低的部门信息
select * from departments where department_id=(
	select department_id from employees GROUP BY department_id HAVING avg(salary)=(
		select min(di.ag) from (
			select avg(salary) ag from employees GROUP BY department_id
		) di
	)
);

3. 查询平均工资最低的部门信息和该部门的平均工资
select *,(select min(di.ag) from (select avg(salary) ag,department_id from employees GROUP BY department_id) di) 平均工资
from departments where department_id=(
	select department_id from employees GROUP BY department_id HAVING avg(salary)=(
		select min(di.ag) from (select avg(salary) ag,department_id from employees GROUP BY department_id) di
	)
);

4. 查询平均工资最高的 job 信息
select * from jobs where job_id=(
	select job_id from employees GROUP BY job_id having avg(salary)=(
		select max(gao.ag) from (select avg(salary) ag,job_id from employees GROUP BY job_id) gao
	)
);

5. 查询平均工资高于公司平均工资的部门有哪些?
select department_id,avg(salary) from employees GROUP BY department_id HAVING avg(salary)>(
select avg(salary) from employees
);

6. 查询出公司中所有 manager 的详细信息.
select * from employees where employee_id in (select DISTINCT emp2.employee_id from employees emp1,employees emp2 where emp1.manager_id=emp2.employee_id);

7. 各个部门中 最高工资中最低的那个部门的 最低工资是多少?
select department_id,(select min(gao.ma) from (select max(salary) ma,department_id from employees GROUP BY department_id) gao ) 最低工资
from employees GROUP BY department_id having min(salary)=(
	select min(gao.ma) from (
		select max(salary) ma,department_id from employees GROUP BY department_id
	) gao 
);

8. 查询平均工资最高的部门的 manager 的详细信息: last_name, department_id, email, salary
select last_name,department_id,email,salary from employees emp where employee_id=(
	select DISTINCT manager_id from employees where department_id=(
		select department_id from employees GROUP BY department_id HAVING avg(salary)=(
			select max(gao.ag) from (
				select avg(salary) ag,department_id from employees GROUP BY department_id
			) gao
		)
	) and manager_id is not null	
);

你可能感兴趣的:(sql)