Aggregate Functions in SQL
SQL aggregate functions such as SUM, AVG, MAX, MIN, COUNT etc. produce a single value for the entire group of table entries. Aggregate or column functions can also be used along with SELECT command.
SUM
Sum function calculates the arithmetic SUM of all selected values of a given column. For example, to find sum of fee paid by students from STUDENTS TABLE we would like to use following command
SELECT SUM(FEE) FROM STUDENTS;
Here fee is a column name in the students table
AVG (AVERAGE)
Using the AVG command we can calculate average of all the selected values of a given column or field. Here one thing is to note that AVG(DISTINCT) eliminates duplicate field values before calculation.
Using same example here we will find average fee paid by using following statement
SELECT AVG(FEE) FROM STUDENT;
MAX
MAX function calculates the largest of all selected values of given column. To check the maximum value in fee column we use following command
SELECT MAX(FEE) FROM STUDENTS;
MIN
MIN function is used to calculate lowest value in a specified field. Taking same example here we will find minimum fee paid by any student. By this statement
SELECT MIN(FEE) FORM STUDENTS;
COUNT
This function counts the number of rows present in the output table. The function COUNT with the star(*) counts the number of rows in the resulting table. Whereas COUNT(DISTINCT) eliminates duplicate fields before counting them in the output table. For example to count the total number of students present in the students table we use following statement:
SELECT COUNT(*)FROM STUDENTS;