SQL WHERE vs HAVING: Filter Rows or Groups?
By Sharon Ben-Moshe · August 16, 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.
WHERE filters rows before aggregation; HAVING filters grouped results. Learn where each clause belongs with a practical sales-report example.
Quick answer
WHERE filters individual rows before grouping and aggregation. HAVING filters groups after aggregate results such as COUNT, SUM, and AVG exist. Use WHERE for input-row conditions, HAVING for aggregate conditions, and use both when a query needs both kinds of filtering.
Keep reading
BigQuery QUALIFY Clause: Filter Window Functions Directly
By Sharon Ben-Moshe · Aug 16, 2026
Use BigQuery QUALIFY to filter window-function results without an outer subquery. Learn evaluation order, top-N-per-group patterns, and portability.
Read postSQL Window Functions vs GROUP BY: Keep Detail Rows
By Sharon Ben-Moshe · Aug 16, 2026
GROUP BY collapses rows; window functions retain detail and add calculations beside it. See when each approach fits, with clear SQL examples.
Read postTry it now
Paste your SQL into sqlfmt — format, validate, and catch typos free in your browser.