我有一个供员工使用的表和另一个用于部门名称的表。 结构是
员工 - 员工ID - 员工姓名 - department_id
部门 - department_id - department_name
我想要显示一个包含超过10名员工的部门名称的表格。
我尝试过没有结果的查询是:
sudo git pull
请建议更正
答案 0 :(得分:2)
在分组之后有一个特定的子句,即HAVING
子句,它允许对聚合值进行过滤。
select count(*) as count,d.department_name
from employees e
inner join departments d on e.department_id = d.department_id
group by d.department_name
HAVING count(*) > 10
注意:where子句应继续用于不依赖于聚合的条件。例如如果你只是想"销售相关"部门
select count(*) as count,d.department_name
from employees e
inner join departments d on e.department_id = d.department_id
WHERE d.department_name like 'sales%'
group by d.department_name
HAVING count(*) > 10