select d.dept_no, d.dept_name, a.sum
from departments d
join (
SELECT de.dept_no, count(s.emp_no) as sum
from dept_emp de
inner join
salaries s
on de.emp_no = s.emp_no
group by de.dept_no
) a
on d.dept_no = a.dept_no
-- 先把2,3表联立,根据薪水表的员工记录数,对应工资数,按部门编号分组。
SELECT de.dept_no, count(s.emp_no) as sum
from dept_emp de
inner join
salaries s
on de.emp_no = s.emp_no
group by de.dept_no
得到的是:
dept, sum,
-- 最后和第一个表联立
得到
dept no, name, sum