HAVING Clause
The HAVING clause is used to filter groups of records created by the GROUP BY clause. It is similar to the WHERE clause but works on aggregated data.
Example: Using HAVING Clause
SELECT City, COUNT(*) FROM FreedomFighters GROUP BY City HAVING COUNT(*) > 2;
Output:
Returns cities with more than 2 freedom fighters.
Do's and Don'ts
Do's
- Use
HAVINGto filter aggregated results fromGROUP BY. - Combine
HAVINGwithGROUP BYfor advanced data analysis.
Don'ts
- Don't use
HAVINGwithout aggregation; useWHEREinstead. - Don't forget to check performance when using
HAVINGon large datasets.