Using Aggregate Functions
Aggregate functions in SQL allow you to perform calculations such as sums, averages, and counting records. They are essential for analyzing and summarizing large volumes of data.
-
COUNT(): Counts the number of rows that meet the condition. -
SUM(): Calculates the sum of values in a specific column. -
AVG(): Calculates the average of the values in a column. -
MIN()andMAX(): Return the minimum and maximum value of a column, respectively.
Syntax
The basic syntax for using aggregate functions in SQL is:SELECT [function]([column]) FROM [table];
SELECT SUM(price) FROM sales;
Aggregate functions include:
COUNT, SUM, AVG, MIN, and MAX.Purpose
Aggregate functions are used to obtain summarized information from data. Examples of their use include:
- Counting the total number of sales made.
- Calculating the average price of products.
- Finding the maximum price of a product.
Exercises
Interactive Test 1: Drag and Drop
Drag in the corresponding order.
Drag the options:
SELECT
SUM
sales
FROM
Completa el código:
______
______
______
______
Interactive Test 2: Sum the Price of Sales.
Rellena los huecos en cada casilla.
SELECT (price) FROM sales;
Practical Exercise:
Statement:
Write an SQL query to calculate the total sales.
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.