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

Drag in the corresponding order.


Drag the options:

SELECT
category
FROM
GROUP BY

Completa el código:

______
______
______
______

Interactive Test 2: Fill in the Blanks

Fill the Blanks.


SELECT category, COUNT(*)
FROM products
GROUP BY ;

Practical Exercise:

Statement:

  Write an SQL query to count the number of products in each category.

Example Data

idnameage
1John25
2Anna30
3Louis22
4Martha35

* Write your SQL query and execute to see results.

Results:

No results available.



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