首页 > 解决方案 > MySQL LEFT JOIN 与 GROUP BY 和 WHERE 子句查询

问题描述

如下所示的表格

SQL 表

如何编写 SQL 查询以读取员工计数薪水 > 1000 的所有部门名称,如果在员工中找不到部门,我们需要在输出中显示为零计数。

标签: sqlleft-join

解决方案


您可以使用left join和聚合:

select d.name, count(e.id)
from departments d left join
     employees e
     on e.department_id = d.id and e.salary >= 10000
group by d.name

推荐阅读