HAVING without GROUP BY is legal but almost always a mistake - the
author usually meant WHERE. The engine treats the whole result set as
one group, which can produce surprising row counts.
Should fail
SELECT total FROM orders HAVING total > 1000;
Should pass
SELECT region, SUM(total)
FROM orders
GROUP BY region
HAVING SUM(total) > 1000;
Implementation hints
- Multi-line rule (
multiline = True, override check_statement).
- Detect
HAVING without a preceding GROUP BY in the same statement.
- Model after
W006 orderby-without-limit which uses the same pattern.
- Severity:
warning.
Estimated LOC: ~30 code + ~20 test.
HAVINGwithoutGROUP BYis legal but almost always a mistake - theauthor usually meant
WHERE. The engine treats the whole result set asone group, which can produce surprising row counts.
Should fail
Should pass
Implementation hints
multiline = True, overridecheck_statement).HAVINGwithout a precedingGROUP BYin the same statement.W006 orderby-without-limitwhich uses the same pattern.warning.Estimated LOC: ~30 code + ~20 test.