Aggregate functions are functions that take a collection of values as input and return a single value.The ISO standard defines five (5) aggregate functions namely :-
1) COUNT
2) SUM
3) AVG
4) MIN
5) MAX
1.COUNT Function
The COUNT function returns the total number of values in the specified field. It works on both numeric and non-numeric data types. All aggregate functions by default exclude nulls values before working on the data.
MIN function
The MIN function returns the smallest value in the specified table field.
2.MAX function
Just as the name suggests, the MAX function is the opposite of the MIN function. It returns the largest value from the specified table field.
3.SUM function
Suppose we want a report that gives total amount of payments made so far. We can use the MySQL SUM function which returns the sum of all the values in the specified column. SUM works on numeric fields only. Null values are excluded from the result returned.
4.AVG function
MySQL AVG function returns the average of the values in a specified column. Just like the SUM function, it works only on numeric data types.
5.MIN function
The MIN function returns the smallest value in the specified table field.
3. Aggregate function
Aggregate functions are functions that take a collection of values as input and return a single value.
► Behavior of Aggregate Functions:
*Operates - on a single column
*Return - a single value.
*Ignore NULL Values
The ISO standard defines five (5) Aggregate Functions
4. 5 built in aggregate functions in SQL
Count01
02
04
03
05
SUM
AVG
MAX
MIN
5. AVG
It returns the average of a data value
Syntax :
SELECT AVG [column-name] FROM [Table-name]
6. E_ID E_Name Age City Salary
E_001 Rakib 24 Dhaka 20,000
E_002 Rafi 21 Savar 18,000
E_003 Razu 30 khulna 35,000
E_004 Rayhan 21 Savar 22,000
E_005 Rahul 19 Sylhet 26,000
Table Name- Employee
Input :
SELECT AVG (Salary) FROM Employee
Example :
Output :
AVG (Salary) = 24,200
7. SUM
It returns the addition of a data value
Syntax :
SELECT SUM [column-name] FROM [Table-name]
8. E_ID E_Name Age City Salary
E_001 Rakib 24 Dhaka 20,000
E_002 Rafi 21 Savar 18,000
E_003 Razu 30 khulna 35,000
E_004 Rayhan 21 Savar 22,000
E_005 Rahul 19 Sylhet 26,000
Table Name- Employee
Input :
SELECT SUM (Salary) FROM Employee
Example :
Output :
SUM (Salary) = 121,000
9. MAX
It returns the maximum value of a column
Syntax :
SELECT MAX [column-name] FROM [Table-name]
10. E_ID E_Name Age City Salary
E_001 Rakib 24 Dhaka 20,000
E_002 Rafi 21 Savar 18,000
E_003 Razu 30 khulna 35,000
E_004 Rayhan 21 Savar 22,000
E_005 Rahul 19 Sylhet 26,000
Table Name- Employee
Input :
SELECT MAX (Age) FROM Employee
Example :
Output :
MAX (Age) = 30
11. MIN
It returns the minimum value of a column
Syntax :
SELECT MIN [column-name] FROM [Table-name]
12. E_ID E_Name Age City Salary
E_001 Rakib 24 Dhaka 20,000
E_002 Rafi 21 Savar 18,000
E_003 Razu 30 khulna 35,000
E_004 Rayhan 21 Savar 22,000
E_005 Rahul 19 Sylhet 26,000
Table Name- Employee
Input :
SELECT MIN (Salary) FROM Employee
Example :
Output :
MIN (Salary) = 18,000
13. COUNT
It returns total number of values in a given
column
Syntax :
SELECT COUNT [column-name] FROM [Table-name]
14. E_ID E_Name Age City Salary
E_001 Rakib 24 Dhaka 20,000
E_002 Rafi 21 Savar 18,000
E_003 Razu 30 khulna 35,000
E_004 Rayhan 21 Savar 22,000
E_005 Rahul 19 Sylhet 26,000
Table Name- Employee
Input :
SELECT COUNT (E_ID) FROM Employee
Example :
Output :
COUNT (E_ID) = 5