Aggregate Functions in MYSQL with examples

It provides the complete details of the functions which are used in MySql to instruct the system to perform any task. These can be used to produce the output or fields from the table based on the requirement.

Example:

MySql COUNT can be used to count the number of rows in the table.

Syntax:

SELECT COUNT (aggregate_expression)
FROM table_name
[WHERE conditions];

Example:

SELECT COUNT(*) FROM employee_tbl
WHERE name="Zara";

Table of aggregate function

Below I have used a table that tells how aggregate function is helpful in using MySql. Its behavior and its syntax is also presented in the table.

MySql COUNT It returns the no of rows SELECT COUNT(aggregate_expression)

FROM Table_name;

[WHERE condition];

MySql SUM Add and returns the value of sum SELECT SUM (aggregate_expression)

FROM Table_name;

[WHERE condition];

MySql AVG It provides the average value SELECT AVG (aggregate_expression)

FROM Table_name;

[WHERE condition];

MySql MIN It gives the smallest value SELECT MIN(aggregate_expression)

FROM Table_name;

[WHERE condition];

MySql MAX It provides the largest value SELECT MAX(aggregate_expression)

FROM Table_name;

[WHERE condition];

MySql FIRST It gives the first value SELECT FIRST(aggregate_expression)

FROM Table_name;

[WHERE condition];

MySql LAST It gives the lat value SELECT LAST (aggregate_expression)

FROM Table_name;

[WHERE condition];

COUNT  function in MYSQL

Syntax:

SELECT COUNT (aggregate_expression)
FROM table_name
[WHERE conditions];
EXAMPLE:
SELECT COUNT(*) FROM employee_tbl
WHERE name="Zara";

SUM function in MYSQL

Syntax

SELECT COUNT (aggregate_expression)
FROM table_name
[WHERE conditions];
EXAMPLE:
SELECT COUNT(*) FROM employee_tbl
WHERE name="Zara";

AVG function in MYSQL

Syntax:

SELECT COUNT (aggregate_expression)
FROM table_name
[WHERE conditions];
EXAMPLE:
SELECT COUNT(*) FROM employee_tbl
WHERE name="Zara";

MIN function in MYSQL

Syntax:

SELECT COUNT (aggregate_expression)
FROM table_name
[WHERE conditions];
EXAMPLE:
SELECT COUNT(*) FROM employee_tbl
WHERE name="Zara";

MAX function in MYSQL

Syntax:

SELECT COUNT (aggregate_expression)
FROM table_name
[WHERE conditions];
EXAMPLE:
SELECT COUNT(*) FROM employee_tbl
WHERE name="Zara";