Skip to content

rule: W021 having-without-group-by - warn on HAVING without GROUP BY #3

@Pawansingh3889

Description

@Pawansingh3889

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions