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

Arrastra en el orden correspondiente.


Arrastra las opciones:

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.

Rellena los huecos en cada casilla.


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.

Datos de Ejemplo

idnameage
1Juan25
2Ana30
3Luis22
4Marta35

* Escribe tu consulta SQL y ejecuta para ver los resultados.

Resultados:

No hay resultados disponibles.



What is the purpose of HAVING in SQL?