×

MySQL Aggregate function

The aggregation function works on the multiple values and returns single value output.  It makes the advanced and complex operation simple. The summation (SUM), MAX, AVG, MIN, COUNT functions are examples of the aggregate function.

  • SUM: The SUM function perform the addition of the table values.
  • MAX: The MAX function determines the maximum value of the data set.
  • AVG: The AVG or average function displays the average value of the data set.
  • MIN: This MIN function shows the minimum value of the data set.
  • COUNT: The COUNT function determines the count of the data set.
  • group_concat(): The group_concat function displays the concatenated string.
  • first(): The first function shows the first value of an expression.
  • last(): The last function displays the last value of an expression.

Syntax

The following syntax is the basis of all aggregate functions.

Aggregate_Function(DISTINCT | ALL)

Description of the aggregate function syntax:

  • The "Aggregate_Function" chooses the required function for MySQL data operation.
  • This function uses the required value or the required column of the table.
  • Sometimes, the aggregation function uses the "DISTINCT" modifier or the "ALL" modifier.
  • The modifier calculates the value of the column and returns the required value of the table column.

Application of the MySQL aggregate function

The aggregation functions need to calculate the maximum, minimum, and relevant values. The following are applications that required aggregate functions to make their uses friendly.

  • The aggregate function is used in school and college management system applications. Here, you can find the highest and lowest marks, summarized mark sheet, and attendance data.
  • The movie and entertainment applications use this function. You can filter movies and dramas as per high and low reviews. You can show the prices of the movie using the aggregate function.
  • The aggregate function is used in business applications like online business and eCommerce sites.
  • It is also used in corporate offices to handle data of the employees and management system.

Prerequisite for the MySQL aggregate function

  • Use database in the MySQL data management system interface. If a database is not available, then create it with the SQL query.
  • Create a new table with the required column, keys, data type, and constraints.

mysql> create table phone_directory (  

    pincode int NOT NULL,

    name varchar(100) NOT NULL,    

    phone_number bigint,  

    received_date date  

);  

  • Insert the value in the table.

mysql> insert into 

phone_directory(pincode, name, phone_number, received_date) 

VALUES    

(411341, 'Ram', 9123011122, '2021-07-01'),  

(412343, 'Sam', 7823019126, '2021-07-03'),  

(412343, 'Tom', 9123056173, '2021-07-05'),  

(413212, 'John', 9425061192, '2021-07-07'),  

(413213, 'Pam', 9128029132, '2021-07-09'),  

(413214, 'Adam', 9823011728, '2021-07-11'); 

  • Show the output table, structure, and its data using the below query.
mysql> select * from phone_directory;
MySQL Aggregate function

SUM() function

The SUM() function shows the addition of the table values. The SUM() function does not return a null value. Here, the sum function shows the summation of all values in the table. If the table displays a null value, then the row is unavailable in the table column. This function works on numerical data type values.

Example: the "SUM" function's example and the output shows below.

mysql > Select SUM(phone_number) AS summation from phone_directory;

OUTPUT

MySQL Aggregate function

The above image shows the summation of the exam hour column. The total addition of the hours displays the "10" value.

MAX() function

The MAX() function returns the maximum value of the data set. This function works on numerical data to display the largest value.

Example: the "MAX" function's example and the output shows below.

mysql > Select MAX(phone_number) AS maximum from phone_directory;

OUTPUT

MySQL Aggregate function

This output shows the largest number of phone numbers in the table. The function compares the entire row with each other.

AVG() or average() function

The average function calculates the average value of the table data. This function works on numerical values because of arithmetic operation. If the table does not contain any value, then the average function shows the null output.

Example: the "average" function's example and the output shows below.

mysql > Select AVG(phone_number) AS average from phone_directory;

OUTPUT

MySQL Aggregate function

The above output shows an average number of the phone_number column. This function returns a decimal value as output because of average calculation.

MIN() function

The MIN() function determines the minimum value of the data set. This function works on numerical data type values. If the table displays a null value, then the column does not contain values.

Example: the "MIN" function's example and the output shows below.

mysql > Select MIN(phone_number) AS minimum from phone_directory;

OUTPUT

MySQL Aggregate function

The above output shows the lowest number of the table column. You can see the lowest phone number of the column shows as output.

COUNT() function

The count() function works for the counting data set. This function displays available rows of the table.  The COUNT function operates string, numerical, and other data type's columns. If the table is empty, then the count function displays the null output.

Example: the "COUNT" function's example shows below.

mysql > Select COUNT(phone_number) AS count from phone_directory;

OUTPUT

MySQL Aggregate function

The count function shows the "6" data set as output. This function returns the available row of the given column.

FIRST function

The first function displays the first value of the column. This function uses the "LIMIT" clause with a one-row condition.  If a table is empty, then output displays a null value. You do not need a special function or keyword for data operation.

Example: the "FIRST" function's example and the output shows below.

mysql> SELECT phone_number from phone_directory LIMIT 1;

OUTPUT

MySQL Aggregate function

The output image shows the first value of the column. The "phone_number" column shows the first row of the given table. You can modify this function with other clauses.

LAST function

The "last" function displays the last value of the table. This function determines the last row of the table column. If the table column is empty, then the output shows a null value. This function requires the "ORDER BY" clause in descending order. The "LIMIT" clause must be containing one (1) value. You do not need a special method and function for the last function.

Example: the "LAST" function's example and the output shows below.

mysql> SELECT phone_number from phone_directory ORDER BY phone_number DESC LIMIT 1;

OUTPUT

MySQL Aggregate function

The above phone number shows you the last row of the table. This value contains the last in the phone number column.

Examples the MySQL aggregate function

The following query shows a different type of MySQL aggregation function. You can understand this function using the below examples. Here, you get the basic information and operation of the aggregation function.

mysql > Select SUM(phone_number) AS SUM,
	  MAX(phone_number) AS MAX,
	  MIN(phone_number) AS MIN,
	 AVG(phone_number) AS Average,
 COUNT(phone_number) AS COUNT from phone_directory;

OUTPUT

MySQL Aggregate function

The above output shows the aggregation function and its value. You can see the difference between the function and its operations.


Related Topics

MySQL LOG2() function

In this context, we will learn how we can use the MySQL LOG2() function to calculate the Logarithm of a specific number with base 2 with proper syntax and examples. Introduction...

2 minutes read.

MySQL DAY() function

In this context, we will learn how we can use the MySQL DAY() function with proper syntax and good examples. Introduction of MySQL DAY() function This function in MySQL is used to...

2 minutes read.

MySQL Advance table Query

MySQL Advance table Query The table is created by index, rows, and columns in the MySQL database. The user saves their information in matrix format. The database requires a query to...

14 minutes read.

MySQL View

Introduction MySQL View is a virtual table to create a clone of the base table. The View does not contain its values or data. MySQL View creates to connect more than...

12 minutes read.

MySQL LOG10() function

In this context, we will learn how we can use the MySQL LOG10() function with proper syntax and good examples. Introduction of MySQL LOG10() function To evaluate the natural logarithmic value of...

2 minutes read.

MySQL SUM function

The SUM() function displays the total addition of the table values. It supports the arithmetic operation of the column values. This function does not return a null value. Here, the...

5 minutes read.

MySQL FIELD() function

In this context, we will learn how we can use the MySQL FIELD() function with proper syntax and good examples. Introduction of MySQL FIELD() function The index position of a mentioned value...

3 minutes read.

MySQL Data Types

Introduction of the Data types In the data file, we have several information such as names, numbers, marks, and other information. This information specifies categories such as numbers, characters, and decimal...

8 minutes read.

MySQL RADIANS() function

In this context, we will learn how we can use the MySQL RADIANS() function with proper syntax and good examples. Introduction of MySQL RADIANS() function RADIANS() function in MySQL is used to...

2 minutes read.

MySQL Composite Index

Composite Index A composite index is an index that is used on multiple columns. MySQL management system manages multiple columns simultaneously. Therefore, the single index contains multiple columns in one query...

3 minutes read.

MySQL: Entity-Relationship Model

Entity-Relationship Model Entity-Relationship model or E R model is used to create a relationship between different attributes or entities. It describes the structure of the database with the help of the...

5 minutes read.

MySQL Aggregate function

The aggregation function works on the multiple values and returns single value output.  It makes the advanced and complex operation simple. The summation (SUM), MAX, AVG, MIN, COUNT functions are...

5 minutes read.

MySQL ISNULL function

This function is used to check the null value in table data. It returns the result in a Boolean form. If table data is null, then the output becomes 1....

2 minutes read.

MySQL Database Introduction

MySQL - Database Introduction: The database plays an essential role in MySQL for storing and modifying the data. The database creates and keeps multiple tables. The database organizes and manipulates...

8 minutes read.

MySQL IF statement

The IF statement shows the true condition of the control flow function. The first condition is necessary to fulfill the requirement. The other condition is optional in the "IF" statement....

3 minutes read.

MySQL Tutorial

In this tutorial, you will get information about the MySQL management system. This tutorial will cover the basic and advanced level MySQL concepts with examples that will help you become...

5 minutes read.

MySQL Table

Introduction MySQL table is an essential part of the system. MySQL table stores data using index, rows, and columns. It accesses data from the server quickly because of the table—this table...

22 minutes read.

MySQL POWER() function

In this context, we will learn how we can use the MySQL POWER() function with proper syntax and good examples. Introduction of MySQL POWER() function The value of a number raised to...

3 minutes read.

MySQL NATURAL JOIN

The natural join combines multiple tables using a column with the same name and data type. This operation combines a row of the two tables using common columns. This join...

4 minutes read.

MySQL MOD() function

In this context, we will learn how we can use the MySQL MOD() function with proper syntax and good examples. Introduction of MySQL MOD() function Basically, in MySQL, the MOD() function is...

3 minutes read.