Aggregate Function
Aggregate Function
Aggregate Function
Syntax:
The following are the syntax to use aggregate functions in MySQL:
Some of the most commonly used aggregate functions are summarised in the below
table:
Aggregate Descriptions
Function
count() It returns the number of rows, including rows with NULL values
in a group.
Example
Suppose we want to get the total number of employees in the employee table, we need to
use the count() function as shown in the following query:
Output:
After execution, we can see that this table has six employees.
Sum() Function
The MySQL sum() function returns the total summated (non-NULL) value of an
expression.
It returns NULL if the result set does not have any rows.
It works with numeric data type only.
Example
Suppose we want to calculate the total number of working hours of all employees in the
table, we need to use the sum() function as shown in the following query:
Output:
AVG() Function
MySQL AVG() function calculates the average of the values specified in the
column.
Similar to the SUM() function, it also works with numeric data type only.
Example
Suppose we want to get the average working hours of all employees in the table, we need
to use the AVG() function as shown in the following query:
Example:
Suppose we want to get minimum working hours of an employee available in the table, we
need to use the MIN() function as shown in the following query:
MAX() Function
MySQL MAX() function returns the maximum (highest) value of the specified column. It
also works with numeric data type only.
Example:
Suppose we want to get maximum working hours of an employee available in the table,
we need to use the MAX() function as shown in the following query: