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 HAVING to filter aggregated results from GROUP BY.
  • Combine HAVING with GROUP BY for advanced data analysis.

Don'ts

  • Don't use HAVING without aggregation; use WHERE instead.
  • Don't forget to check performance when using HAVING on large datasets.