SQL22 统计各个部门的工资记录数
我的思路: 需求搞清楚,就蛮简单了,先将部门员工表和薪水表连接,然后通过部门编号分组,计算数据条数,可以得出每个部门对应的薪水记录数,然后和部门表连起来,即可获取对应的部门名称了。
我的题解:
select a1.dept_no, b1.dept_name, a1.sum from ( select a.dept_no, count(a.emp_no) as sum from dept_emp as a join salaries as b on a.emp_no = b.emp_no group by a.dept_no) as a1, departments as b1 where a1.dept_no = b1.dept_no;
涉及知识点:
- 完整sql执行顺序(每天看一遍,不信记不住):
from -> where -> group by -> having -> select -> order by -> limit
https://mp.weixin.qq.com/s/e8FI__8mHJzLyWNHcAMrJA