GROUP BY and HAVING: Classic SQL Beginner Mistakes
Why your query returns an aggregation error, and how to correctly filter a grouped result.
SQL · 7 min read
By the FormulAI editorial team · Reviewed October 2026
WHERE Filters Before, HAVING Filters After
WHERE applies to raw rows before grouping, while HAVING applies to the aggregated result. Filtering a SUM() within a WHERE clause always causes an error: the sum does not yet exist at that stage of execution.
Every Non-Aggregated Column Must Be Grouped
If you select customer_name and SUM(amount), customer_name must appear in the GROUP BY clause. PostgreSQL will reject the query, while MySQL in permissive mode might return an arbitrary value—which is worse, as the error becomes silent.
Counting Correctly
COUNT(*) counts rows, COUNT(column) ignores NULLs, and COUNT(DISTINCT column) counts unique values. Three different results for three different business needs.
Readability
Name your aggregates using AS (e.g., SUM(amount) AS revenue): CSV exports and reporting tools use these aliases as column headers.
How this guide was made
Every formula, query and script in this guide was run on a real sample file (Excel 365, Google Sheets, PostgreSQL, MySQL or Python 3) before publication. We document the edge cases we hit during testing, such as text-formatted numbers, empty months or NULL values. AI helps us draft and translate; a human checks each example and updates the guide when software versions change. Spotted an error? Write to contact@formulai-app.com.