我需要知道如何编写查询来显示deptno和该部门中的emp计数,仅显示拥有超过5名员工的depts。我试过这个:
SELECT D.DNAME FROM
dept D WHERE (SELECT COUNT(*)
FROM emp E
WHERE E.DEPTNO = D.DEPTNO) > 3
虽然我只得到了部门
答案 0 :(得分:3)
您可以使用HAVING
子句在聚合后过滤行:
SELECT d.dname, COUNT(*) AS num_of_emp
FROM dept d
JOIN emp e
ON d.deptno = e.deptno
GROUP BY d.dname
HAVING COUNT(*) > 5