Aggregate functions in MariaDB are used to perform calculations on multiple rows of a single column of a table and return a single value. They come handy when small pieces of information, such as the total amount or average amount, need to be quickly extracted from a large volume of data.
Here are some commonly used aggregate functions:
- `COUNT`: Returns the total number of rows that match a specified criteria.
- `SUM`: Returns the combined sum of all rows that match a specified criteria.
- `AVG`: Returns the average value of a numeric column.
- `MIN`: Returns the smallest value of the selected column.
- `MAX`: Returns the largest value of the selected column.
Here is how they can be used:
1. `COUNT` function:
```
SELECT COUNT
FROM table_name;
```
The COUNT function returns the total number of rows in the table.
1. `SUM` function:
```
SELECT SUM
FROM table_name;
```
The SUM function returns the total sum of all values in the specified column.
1. `AVG` function:
```
SELECT AVG
FROM table_name;
```
The AVG function returns the average value of a numeric column.
1. `MIN` function:
```
SELECT MIN
FROM table_name;
```
The MIN function returns the smallest value of the selected column.
1. `MAX` function:
```
SELECT MAX
FROM table_name;
```
The MAX function returns the largest value of the selected column.
You may also combine aggregate functions with the `GROUP BY` statement to retrieve information about a group of rows:
```
SELECT COUNT, column2_name
FROM table_name
GROUP BY column2_name;
```
In this query, MariaDB counts the number of rows for each distinct value in column2\_name.
Remember to replace “table_name” and “column_name” with your actual table and column names.