1、inner join
1、内连接:只取两张表有对应关系的记录
select * from emp2 inner join dep2 on emp2.dep_id = dep2.id;
select * from emp2 inner join dep2 on emp2.dep_id = dep2.id and dep2.name = ‘技术’;
2、left join
2、左连接: 在内连接的基础上保留左表没有对应关系的记录
select * from emp2 left join dep2 on emp2.dep_id = dep2.id;
3、right join
3、右连接: 在内连接的基础上保留右表没有对应关系的记录
select * from emp2 right join dep2 on emp2.dep_id = dep2.id;
4、union
4、全连接:在内连接的基础上保留左、右面表没有对应关系的的记录
select * from emp2 left join dep2 on emp2.dep_id = dep2.id
union
select * from emp2 right join dep2 on emp2.dep_id = dep2.id;
2、子查询
```mysql
#子查询就是将一个查询语句的结果用括号括起来,当做另一个查询语句的条件去用
#1.查询部门是技术或者人力资源的员工信息
'''
先获取技术部和人力资源的id号,再去员工表里根据前面的id筛选出符合要求的员工信息;
'''
select * from emp2 where dep_id in (select id from dep2 where name='技术' or name='人力资源');
#2.每个部门最新入职的员工 思路:先查每个部门最新入职的员工,再按部门对应上联表查询
#查第一张emp表
select t1.id, t1.name, t1.hire_date, t1.post, t2.* from emp as t1
inner join
(select post, max(hire_date) as max_date from emp group by post) as t2
on t1.post = t2.post where t1.hire_date = t2.max_date;