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.
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
| id | name | age |
|---|---|---|
| 1 | Juan | 25 |
| 2 | Ana | 30 |
| 3 | Luis | 22 |
| 4 | Marta | 35 |
* Escribe tu consulta SQL y ejecuta para ver los resultados.
Resultados:
No hay resultados disponibles.