把三个先用inner join 全部联结起来,用with clause把联结起来的表暂时取名t1。然后就 group by + count,最后根据 dept_no order一下答案就出来了。推荐使用with clause,搞一些subtable 简洁高效,增加代码可读性。

with t1 as (
    select dept_emp.dept_no as dept_no, departments.dept_name as dept_name, salaries.salary as salary
    from dept_emp join salaries 
    on dept_emp.emp_no = salaries.emp_no
    join departments
    on departments.dept_no = dept_emp.dept_no
)


select dept_no, dept_name, count(salary) as sum from t1
group by dept_no
order by dept_no asc