×

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 Vs MongoDB

What is MongoDB? MongoDB is a distributed, open-source, cross-platform document-based database which was created to scale and develop applications simply. It was created as a NoSQL database by MongoDB Inc. MongoDB gets...

6 minutes read.

MySQL logical conditions

MySQL logical conditions Introduction MySQL handles data with clauses, operators, and conditions. The logical condition is used to compare information and returns the required output. This condition applies logic to MySQL expressions...

7 minutes read.

MySQL INNER JOIN

MySQL inner join connects two tables by using their common columns. It is the basic join of the MySQL system. It is used to returns only those results from the...

4 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 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 String Function

The string function is used to maintain and operate string values. This function modifies the string data as per function. We can do the concatenation, conversion, removal and replacing the...

4 minutes read.

MySQL Command line client Basic Queries

MySQL – command line client Basic Queries There are many queries to work with the MySQL command-line client. Today, we will explore MySQL basic queries and their function. The MySQL query...

8 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 CEIL() function

In this context, we will learn how we can use the MySQL CEIL () function with proper syntax and good examples. Introduction of MySQL CEIL() function Input is taken by the CEIL()...

3 minutes read.

MySQL Features

Features of MySQL MySQL is a relational database management system (RDBMS): It is a collection of many programs and makes relations with many other programs. Easy to use MySQL database: MySQL...

2 minutes read.

MySQL Truncate() Function

In this context, we will learn how we can use the MYSQL TRUNCATE function to truncate a number to a mentioned number of decimal places. Syntax of MySQL Truncate Function A number...

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 CROSS JOIN

MySQL CROSS JOIN combines all possibilities of the two or more tables and returns the result that contains every row from all contributing tables. It links several columns from the...

4 minutes read.

MySQL Clauses

MySQL Clauses MySQL system clauses are keywords or statements to handle information. It helps to operate a group of the data and apply it to require conditions. The clauses apply conditions...

16 minutes read.

MySQL RTRIM() function

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

2 minutes read.

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 RIGHT JOIN

MySQL right join links two tables with each other. The left table column connects with the complete right side table. Each row of the right table tries to connect with...

4 minutes read.

MySQL FLOOR() function

In this context, we will learn how we can use the MySQL FLOOR() function with proper syntaxes and examples. Introduction of MySQL Floor() function: FLOOR() function in MySQL is used to return...

3 minutes read.

MySQL PERIOD_ADD() function

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

2 minutes read.

MySQL Unique index

It helps to maintain data integrity to enforce the uniqueness of values in one or more columns. We can create more than one UNIQUE index in a single table, which...

3 minutes read.