Which fix removes the bug?
SELECT dept, COUNT(*) FROM emp WHERE COUNT(*) > 5 GROUP BY dept;
- Use
COUNT(dept)in WHERE - Move the test into
HAVING - Put WHERE after the GROUP BY
- Alias it:
COUNT(*) AS n
WHERE filters rows before grouping, so aggregates are not allowed there; HAVING filters the groups.
Play Problem Solving ▸