Aggregate Function

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

MySQL Aggregate Functions

MySQL's aggregate function is used to perform calculations on multiple values and


return the result in a single value like the average of all values, the sum of all values,
and maximum & minimum value among certain groups of values. We mostly use the
aggregate functions with SELECT statements in the data query languages.

Syntax:
The following are the syntax to use aggregate functions in MySQL:

SELECT function_name (DISTINCT | ALL expression) FROM TABLE_NAME;

In the above syntax, we had used the following parameters:

o First, we need to specify the name of the aggregate function.


o Second, we use the DISTINCT modifier when we want to calculate the result based
on distinct values or ALL modifiers when we calculate all values, including
duplicates. The default is ALL.
o Third, we need to specify the expression that involves columns and arithmetic
operators.

There are various aggregate functions available 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.

sum() It returns the total summed values (Non-NULL) in a set.

average() It returns the average value of an expression.

min() It returns the minimum (lowest) value in a set.

max() It returns the maximum (highest) value in a set.


Count() Function
 MySQL count() function returns the total number of values in the expression. This
function produces all rows or only some rows of the table based on a specified
condition.
 It returns zero if it does not find any matching rows.
 It can work with both numeric and non-numeric data types.

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:

mysql> SELECT COUNT(name) FROM employee;

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:

mysql> SELECT SUM(working_hours) AS "Total working hours" FROM employee;

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:

mysql> SELECT AVG(working_hours) AS "Average working hours" FROM employee;


MIN() Function
MySQL MIN() function returns the minimum (lowest) value of the specified column. It
also works with numeric data type only.

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:

mysql> SELECT MIN(working_hours) AS Minimum_working_hours FROM employee;

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:

mysql> SELECT MAX(working_hours) AS Maximum_working_hours FROM employee;

You might also like