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

idnameage
1Juan25
2Ana30
3Luis22
4Marta35

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

Resultados:

No hay resultados disponibles.



Which of the following statements is correct for grouping results in SQL?