Advanced Filters with HAVING


  The HAVING filter in SQL allows you to filter result groups after they have been grouped using GROUP BY. It's essential for analyzing aggregated data.


Syntax


The basic syntax for using HAVING in SQL is:
SELECT [column], COUNT(*) FROM [table] GROUP BY [column] HAVING [condition];
Operators you can use in the HAVING condition include: >, <, =, etc.


Purpose


HAVING is essential for:


  • Filtering aggregation results.
  • Counting elements within groups.
  • Applying conditions to grouped results.


Exercises



Interactive Quiz 1: Drag and Drop

Drag in the corresponding order.


Drag the options:

SELECT
name
GROUP BY
HAVING
COUNT()
FROM
products

Completa el código:

______
______
______
______
______
______
______

Interactive Quiz 2: Select the number and names of employees grouped by age greater than 20.

Fill the Blanks.


SELECT name, COUNT(*) FROM employees
GROUP BY age
HAVING COUNT(*) ;

Practical Exercise:

Statement:

  Write an SQL query that selects the name of products with a total sales greater than 50.

Example Data

idnameage
1John25
2Anna30
3Louis22
4Martha35

* Write your SQL query and execute to see results.

Results:

No results available.



What is the purpose of HAVING in SQL?