SQL WHERE vs HAVING

Galaxy Glossary

What is the difference between SQL WHERE and HAVING clauses?

WHERE filters rows before aggregation, HAVING filters aggregated groups after GROUP BY.

Sign up for the latest in SQL knowledge from the Galaxy Team!

Description

SQL WHERE vs HAVING

Understand when to filter rows with WHERE and when to filter aggregated groups with HAVING.

What Is the Difference Between WHERE and HAVING in SQL?

WHERE evaluates each row before any GROUPBY or aggregate function runs, while HAVING evaluates the result set after aggregation. Because HAVING sees grouped data, it can reference aggregate functions such as COUNT(), SUM(), or AVG(), which WHERE cannot.

When Should I Use WHERE vs HAVING?

Use WHERE for non-aggregate conditions like date ranges, status flags, or simple comparisons. Switch to HAVING when you must filter on aggregate results—for example, “customers with more than 5 orders” or “products whose total sales exceed $10 000.”

Does HAVING Filter Before or After GROUP BY?

HAVING executes after GROUPBY. The database groups rows, calculates aggregates, then applies HAVING. This order lets HAVING access summary values but also means unnecessary grouping happens if row-level filters were not applied earlier with WHERE.

Which Clause Performs Better?

WHERE usually outperforms HAVING because it reduces the data set earlier, allowing the optimizer to skip grouping unwanted rows. Always push row-level predicates into WHERE first, then reserve HAVING for true aggregate filters.

How Do Aggregates Affect Clause Choice?

Aggregates like COUNT(), SUM(), MIN(), and MAX() cannot appear in WHERE. If your condition requires one of these functions, place it in HAVING. For mixed cases, combine both clauses: put simple filters in WHERE and aggregate filters in HAVING.

Practical Example of WHERE and HAVING

The query below finds active customers created after 2023-01-01 who placed more than five orders:SELECT customer_id,
COUNT(*) AS order_count
FROM orders
WHERE customer_status = 'active'
AND created_at > '2023-01-01'
GROUP BY customer_id
HAVING COUNT(*) > 5;
WHERE filters rows by status and date first; HAVING then filters grouped customers by order_count.

Best Practices for WHERE and HAVING

Always place row-level predicates in WHERE to leverage indexes. Only use HAVING for conditions that reference aggregates. Combine both clauses for optimal performance and clarity. Document your choice so teammates understand execution order, especially when collaborating in tools like Galaxy.

Why SQL WHERE vs HAVING is important

Filtering at the proper stage cuts query runtimes, lowers compute cost, and ensures accurate results. Misplacing predicates forces the database to group unnecessary rows, slows dashboards, and inflates cloud bills. Mastering WHERE vs HAVING is foundational for performant analytics pipelines and is frequently tested in technical interviews.

SQL WHERE vs HAVING Example Usage


SELECT department, AVG(salary) AS avg_salary FROM employees WHERE active = TRUE GROUP BY department HAVING AVG(salary) > 90000;

SQL WHERE vs HAVING Syntax



Common Mistakes

Frequently Asked Questions (FAQs)

FAQ

Can I use HAVING without GROUP BY?

Yes, but it’s rare. Without GROUP BY, HAVING acts like a WHERE on the aggregated result set, usually returning one row.

What happens if I place the same condition in WHERE and HAVING?

The optimizer may deduplicate, but it’s redundant and can confuse readers. Keep each predicate where it belongs.

How can Galaxy help me choose between WHERE and HAVING?

Galaxy’s AI copilot flags aggregate misuse, suggests moving filters to WHERE, and visualizes the execution plan so you can verify performance.

Does HAVING support window functions?

No. Use window functions in SELECT or a subquery, then filter the outer query’s results with WHERE.

Want to learn about other SQL terms?

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