Grouping results with GROUP BY
Using GROUP BY
in SQL allows you to group results based on one or more columns. This is useful for performing aggregations such as counting, summing, or averaging data.
Syntax
The basic syntax for using GROUP BY
in SQL is:SELECT [columns] FROM [table] GROUP BY [column];
You can use GROUP BY
along with aggregate functions like COUNT
, SUM
, and AVG
.
Purpose
Grouping results is essential for data analysis. It allows you to:
- Count how many products are in each category.
- Calculate the total revenue for each product type.
- Get the average price per category.
Exercises
Interactive Test 1: Drag and Drop
Arrastra en el orden correspondiente.
Arrastra las opciones:
SELECT
category
FROM
GROUP BY
Completa el código:
______
______
______
______
Interactive Test 2: Fill in the Blanks
Rellena los huecos en cada casilla.
SELECT category, COUNT(*) FROM products GROUP BY ;
Practical Exercise:
Statement:
Write an SQL query to count the number of products in each category.
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.