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() and MAX(): 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

Arrastra en el orden correspondiente.


Arrastra las opciones:

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

idnameage
1Juan25
2Ana30
3Luis22
4Marta35

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

Resultados:

No hay resultados disponibles.



Which of the following is an aggregate function in SQL?