# 题目想获取,每个部门的不同title的分配人数
# 分组:部门编号、部门名称,titile
# 聚合:员工编号
# 思路:将3个表 join 成大宽表,group by 即可,titles 为左表

select dem.dept_no
      ,dep.dept_name
      ,ti.title
      ,count(ti.emp_no) as title_count
from titles as ti
left join dept_emp as dem
on ti.emp_no=dem.emp_no
left join departments as dep
on dem.dept_no=dep.dept_no
where dem.to_date='9999-01-01'
and ti.to_date='9999-01-01'
group by dem.dept_no
        ,dep.dept_name
        ,ti.title
order by dem.dept_no,ti.title