What Is SQL HAVING?

SQL HAVING filters grouped rows after aggregation. Unlike WHERE, which filters individual rows before GROUP BY, HAVING applies conditions to aggregate results such as COUNT, SUM, AVG, MIN, and MAX. Use HAVING to keep only those groups that match specific aggregate criteria, combine it with GROUP BY, and avoid performance issues by limiting row volume early with WHERE.

Learning
June 10, 2025
Galaxy Team
Sign up for the latest notes from our team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.
SQL HAVING filters groups produced by GROUP BY using aggregate conditions such as COUNT > 10 or SUM(sales) < 1000. WHERE cannot reference aggregates; HAVING can. Place WHERE before GROUP BY, then HAVING.

What Does the SQL HAVING Clause Do?

SQL HAVING filters grouped rows after aggregation, keeping only groups that satisfy aggregate conditions like COUNT(>10) or SUM(<1000).

Why Use HAVING Instead of WHERE?

WHERE filters individual rows before grouping, so it cannot reference aggregates. HAVING evaluates after GROUP BY, allowing conditions on COUNT, SUM, AVG, MIN, or MAX.

How Does HAVING Work in the Query Order of Operations?

SQL logically executes FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY. HAVING sees aggregate results that WHERE never sees, making it ideal for post-aggregation filtering.

What Is the Basic Syntax of HAVING?

Place HAVING after GROUP BY: SELECT col, COUNT(*) FROM table GROUP BY col HAVING COUNT(*) > 5;. The clause can include multiple conditions joined by AND/OR.

Can HAVING Filter on Non-Aggregated Columns?

Yes, but only if the column also appears in the GROUP BY list. Otherwise, the database cannot uniquely identify the value for each group and throws an error.

How Do I Combine WHERE and HAVING?

Use WHERE to cut down rows early for speed, then HAVING to filter the aggregated result. Example: first restrict a date range with WHERE, then apply HAVING COUNT(>100).

Can I Use HAVING Without GROUP BY?

Yes. When no GROUP BY exists, the entire result set is treated as one group. HAVING can then filter on aggregates like SUM(sales) > 1000.

How Do Multiple Conditions Work in HAVING?

Chain conditions with AND or OR: HAVING SUM(sales) > 10000 AND AVG(discount) < 0.05. Parentheses control precedence for complex logic.

What Performance Tips Apply to HAVING?

Filter as much as possible in WHERE to reduce rows before GROUP BY, index columns used in WHERE and GROUP BY, and avoid putting non-aggregated columns in HAVING.

How to Replace HAVING With Window Functions?

Window functions let you keep detailed rows while filtering on aggregates. Use COUNT(*) OVER (PARTITION BY col) in a subquery, then apply WHERE to that derived table.

Best Practices for Using HAVING

Always pair HAVING with GROUP BY unless filtering the entire set, combine WHERE and HAVING for speed, avoid SELECT *, and document why the aggregate thresholds were chosen.

When Should I Avoid HAVING?

Avoid HAVING when simple WHERE can do the job, such as filtering on non-aggregate columns. Using HAVING unnecessarily forces the database to aggregate before filtering.

Key Takeaways on SQL HAVING

HAVING filters groups after aggregation, complements WHERE, uses aggregate functions in conditions, and should be combined with indexing and early filtering for optimal performance.

Next Steps for Learning

Practice rewriting reports that currently use WHERE into GROUP BY/HAVING queries, explore window functions, and test performance differences in your database.

Frequently Asked Questions (FAQs)

When should I use HAVING vs WHERE?

Use WHERE to filter individual rows before aggregation. Use HAVING to filter groups based on aggregate results like COUNT or SUM.

Can I reference AVG() in WHERE?

No. Aggregate functions are evaluated after WHERE. Place AVG() conditions in HAVING.

Is HAVING slower than WHERE?

HAVING can be slower because it processes data after GROUP BY. Combine it with WHERE to reduce rows early.

Can HAVING be used without GROUP BY?

Yes. Without GROUP BY, the entire result is one group, and HAVING filters that single aggregate row.

Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Check out our other posts!

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo