The HAVING clause is used in SQL to filter aggregated data. It works with GROUP BY to restrict the groups of rows returned, based on conditions on aggregate functions like COUNT(), SUM(), AVG(), etc.


✅ Syntax

SELECT column1, AGG_FUNC(column2)
FROM table_name
GROUP BY column1
HAVING condition;

📘 Difference Between WHERE and HAVING

ClauseUsed ForWorks On
WHEREFilter rows before groupingRaw table data
HAVINGFilter groups after aggregationGrouped results

🎓 Example Table: orders

order_idcustomer_idamount
1101500
2102300
3101400
4103700
5102200

🔍 Query Example: Total Order Amount Per Customer

SELECT customer_id, SUM(amount) AS total_spent
FROM orders
GROUP BY customer_id;

Result:

customer_idtotal_spent
101900
102500
103700

🚨 Apply HAVING: Customers Who Spent More Than 600

SELECT customer_id, SUM(amount) AS total_spent
FROM orders
GROUP BY customer_id
HAVING SUM(amount) > 600;

Result:

customer_idtotal_spent
101900
103700

🧠 Combine WHERE and HAVING

SELECT customer_id, COUNT(*) AS order_count
FROM orders
WHERE amount > 200
GROUP BY customer_id
HAVING COUNT(*) >= 2;

This filters out low-value orders first, then counts remaining orders per customer, and finally filters those with at least 2 such orders.


🛠 Use with Other Aggregate Functions

SELECT customer_id, AVG(amount) AS avg_order
FROM orders
GROUP BY customer_id
HAVING AVG(amount) >= 400;

📌 Tips and Best Practices

  • Use HAVING only when dealing with aggregate results (SUM, AVG, etc.)
  • Use WHERE to pre-filter data before aggregation
  • You can alias aggregate functions (e.g., AS total) and use those in HAVING (but not in all databases — MySQL supports it)

🎯 Summary

ClausePurposeExample Use
WHEREFilter rowsWHERE amount > 100
HAVINGFilter groupsHAVING SUM(amount) > 500

The HAVING clause is used in SQL to filter aggregated data. It works with GROUP BY to restrict the groups of rows returned, based on conditions on aggregate functions like COUNT(), SUM(), AVG(), etc.


✅ Syntax

SELECT column1, AGG_FUNC(column2)
FROM table_name
GROUP BY column1
HAVING condition;

📘 Difference Between WHERE and HAVING

ClauseUsed ForWorks On
WHEREFilter rows before groupingRaw table data
HAVINGFilter groups after aggregationGrouped results

🎓 Example Table: orders

order_idcustomer_idamount
1101500
2102300
3101400
4103700
5102200

🔍 Query Example: Total Order Amount Per Customer

SELECT customer_id, SUM(amount) AS total_spent
FROM orders
GROUP BY customer_id;

Result:

customer_idtotal_spent
101900
102500
103700

🚨 Apply HAVING: Customers Who Spent More Than 600

SELECT customer_id, SUM(amount) AS total_spent
FROM orders
GROUP BY customer_id
HAVING SUM(amount) > 600;

Result:

customer_idtotal_spent
101900
103700

🧠 Combine WHERE and HAVING

SELECT customer_id, COUNT(*) AS order_count
FROM orders
WHERE amount > 200
GROUP BY customer_id
HAVING COUNT(*) >= 2;

This filters out low-value orders first, then counts remaining orders per customer, and finally filters those with at least 2 such orders.


🛠 Use with Other Aggregate Functions

SELECT customer_id, AVG(amount) AS avg_order
FROM orders
GROUP BY customer_id
HAVING AVG(amount) >= 400;

📌 Tips and Best Practices

  • Use HAVING only when dealing with aggregate results (SUM, AVG, etc.)
  • Use WHERE to pre-filter data before aggregation
  • You can alias aggregate functions (e.g., AS total) and use those in HAVING (but not in all databases — MySQL supports it)

🎯 Summary

ClausePurposeExample Use
WHEREFilter rowsWHERE amount > 100
HAVINGFilter groupsHAVING SUM(amount) > 500