语法:
select 查询列表
from 表1 别名 【连接类型】
join 表2 别名
on 连接条件
【where 筛选条件】
【group by 分组】
【having 筛选条件】
【order by 排序列表】
分类:
内连接:inner
外连接:左外 :left 【outer】 右外:right 【outer】全外:full【outer】
交叉连接:cross
内连接:
语法:
select 查询列表
from 表1 别名
inner join 表2 别名
on 连接条件
等值连接:
案例一:
查询员工名,部门名
select last_name,department_name
from employees e
inner join departments d
on e.department_id=d.department_id;
案例二:
查询名字中包含e的员工名和工种名(添加筛选条件)
select last_name ,job_title
from employees e
inner join job j
on e.job_id =j.job_id
where e.last_name like '%e%';
查询部门个数>3的城市名和部门个数
分析:1.查询每个城市的部门个数
2.在1成立的条件下满足>3的
select city,count(*) 部门个数
from department d
inner join location l
on d.location_id =l.location_id
group by city
having count(*)>3;
案例四:
查询哪个部门的员工的个数大于3的部门名和员工个数,并且按个数进行升序
select count(*) ,department_name
from employees e
inner join departments d
on e.department_id=d.department_id
group by department_name
having count(*)>3
order by count(*) desc;
查询员工名,部门名,工种名,并按部门名降序
select last_name,department_name,job_title
from employees e
inner join departments d
on e.department_id=d.department_id
inner join jobs j on e.job_id=j.job_id
order by department_name desc;