我目前有以下SQL查询,其中列出了在雇佣分析师的部门工作的所有员工的所有姓名。
SELECT ename
FROM emp
WHERE deptno IN (SELECT DISTINCT emp.deptno
FROM emp, dept
WHERE emp.deptno = dept.dept
AND job = 'Analyst');
如何修改它以显示在雇用两名或更多分析师的部门工作的所有员工的姓名?
答案 0 :(得分:2)
您可以使用GROUP BY
获取每个部门的分析员数量,然后根据此数量过滤员工:
SELECT emp.ename
FROM
emp, (
SELECT emp.deptno, count(*) as analyst_count
FROM emp, dept
WHERE emp.deptno = dept.dept
AND job = 'Analyst'
GROUP BY emp.deptno
) foo
WHERE emp.deptno = foo.deptno
AND foo.analyst_count >= 2;
答案 1 :(得分:1)
使用GROUP BY
和HAVING
SELECT emp.deptno
FROM emp, dept
WHERE emp.deptno = dept.dept AND job = 'Analyst'
GROUP BY emp.deptno HAVING COUNT(*) > 1