Site icon TestingDocs.com

MySQL Aggregate Functions

Overview

MySQL Aggregate Functions are functions that operate on a group of rows. Aggregate functions allows us to know about the overall characteristics of the rows, interested in getting a summary on selected rows etc.

MySQL Aggregate Functions

Aggregate functions perform summary operations on a set of values, such as counting the number of rows, averaging, or finding the minimum or maximum values. They calculate a single value based on a group of values from different rows.

Some of the example functions are listed below:

MySQL Aggregate Function Description
SUM() To compute the sum or total value of the column
MIN() To find the smallest value
MAX() To find the largest value
COUNT() To count the rows with non-null values, or the number of distinct values
AVG() To compute the average of the column values

 

SUM()

SUM aggregate function computes the total of the values of the aggregated column values.

In the following example, the SUM() aggregate function is used to get the total population of all
the continents in the country table of the world MySQL database.

mysql> SELECT SUM(Population) FROM Country ;
+—————–+
| SUM(Population) |
+—————–+
| 6078749450 |
+—————–+
1 row in set (0.00 sec)

 

MIN()

The MIN() aggregate function selects the minimum value for the column based on group of rows in the table.

MAX()

The MAX() aggregate function selects the maximum value for the column based on group of rows in the table.

COUNT()

The COUNT() aggregate function is used to get the count of the number of rows in the table. In the following example, the COUNT (*) aggregate function is used to get the count of all rows in the City table of the world MySQL database.

mysql> SELECT COUNT(*) FROM City;
+———-+
| COUNT(*) |
+———-+
| 4079 |
+———-+
1 row in set (0.01 sec)

The NULL values are not included in the COUNT function.

AVG()

AVG() aggregate function is used to compute the average value of the group of values in the table.

MySQL Tutorials

MySQL Tutorials on this website:

https://www.testingdocs.com/mysql-tutorials-for-beginners/

 

Exit mobile version