MySQL Functions

Aggregate Functions

MYSQL aggregate functions return a single value, calculated from values in a column. Useful aggregate functions:

  • AVG() – Returns the average value
  • COUNT() – Returns the number of rows
  • MAX() – Returns the largest value
  • MIN() -Returns the smallest value
  • SUM() -Returns the sum

AVG () Function:

The AVG () function returns the average value of a numeric column.

AVG () Syntax SELECT AVG (column_name) FROM table_name;

Ex:

SELECT AVG (Price) FROM Products;

COUNT () Function:

COUNT aggregate function is used to count the number of rows in a database table.

COUNT () Syntax:

SELECT COUNT (column_name) FROM table_name;

Ex:

SELECT COUNT (Price) FROM Products;

MAX () Function:

The SQL MAX aggregate function allows us to select the highest (maximum) value for a certain column.

MAX () Syntax:

SELECT MAX (column_name) FROM table_name;

EX:

SELECT MAX (SALARY) FROM EMP;

SQL MIN Function:

SQL MIN function is used to find out the record with minimum value among a record set.

MIN() Syntax:

SELECT MIN (column_name) FROM table_name;

EX:

SELECT MIN (SALARY) FROM EMP;

SQL SUM Function:

SQL SUM function is used to find out the sum of a field in various records.

SUM () Syntax:

SELECT COUNT (column_name) FROM table_name;

EX:

SELECT COUNT (EID) FROM EMP;