How to Format the SQL HAVING Clause: Multi-Condition Group Filters
By Sharon Ben-Moshe · October 12, 2026 · 1 min read
Sharon Ben-Moshe is the founder of sqlfmt, a browser-based SQL formatter and validator with misspelled-keyword suggestions across PostgreSQL, MySQL, SQLite, SQL Server, and BigQuery.
Master formatting standards for the SQL HAVING clause. Learn how to format aggregate filters, multiple AND/OR conditions, and alignment with GROUP BY.
Quick answer
Format the SQL HAVING clause immediately after the GROUP BY column list, left-aligned with other primary query keywords (SELECT, FROM, WHERE, GROUP BY). When combining multiple aggregate conditions, indent each filter expression 2 spaces and place boolean operators (AND, OR) at the beginning of each newline for easy scanning and editing.
FAQs
Can HAVING be used without a GROUP BY clause?
Yes! When aggregating an entire table without GROUP BY (e.g. `SELECT AVG(score) FROM tests HAVING COUNT(*) > 10;`), HAVING filters the single summary row.
Why should boolean operators (AND/OR) lead newlines in HAVING?
Leading operators allow developers to comment out individual conditions easily during debugging and make logical relationships visible without horizontal scanning.
Can column aliases be used in the HAVING clause?
In PostgreSQL and SQL Server, aliases from SELECT cannot be used in HAVING (you must repeat the aggregate expression like `HAVING COUNT(*) > 5`). In MySQL and BigQuery, SELECT aliases are permitted in HAVING.
Should row filters go in WHERE or HAVING?
Filters on individual raw columns belong in WHERE before grouping. Only conditions that evaluate aggregated summary metrics (COUNT, SUM, AVG) belong in HAVING.
Keep reading
Building an Enterprise SQL Style Guide: Formatting Standards, Linting, and Team Conventions
By Sharon Ben-Moshe · Oct 13, 2026
The complete blueprint for an enterprise SQL style guide. Standardize formatting, keyword casing, indentation, naming conventions, and CI linting.
Read postThe Master Guide to SQL Common Table Expressions (CTEs): Syntax, Optimization Fences, and Recursion
By Sharon Ben-Moshe · Oct 13, 2026
The definitive guide to SQL Common Table Expressions (CTEs). Master chaining, recursive hierarchies, data-modifying CTEs, and optimization fences.
Read postTry it now
Paste your SQL into sqlfmt — format, validate, and catch typos free in your browser.