What is Aggregate Functions in SQL?
SQL aggregation function is used to perform the calculations on multiple tuples of a single column of a table. After the calculation, It returns a single value.
SQL aggregation function is also used to summarize the data.
Aggregate function is only applicable to a numeric column. If you apply sum function on non numeric column such as text, char or string datatype columns, it fails and query will throw an error.
Keep in mind that, whenever you use aggregate function in SQL query and also fetching other columns than you mush also used GROUP BY.
Types of Aggregation function in SQL
There are mainly five types of aggregation function in SQL are as follows
- SUM()
- MIN()
- MAX()
- AVG()
- COUNT()
Syntax of Aggregate Functions
Following is the syntax of Aggregate function, it gives you an idea about, how you can use while writing a SQL query.
// Single column in select query
SELECT AGGREGATE_FUNCTION(column_name) FROM table_name;
// When you used Multiple columns in select query, GROUP BY is must
SELECT AGGREGATE_FUNCTION(column_name_1), column_name_2 FROM table_name
GROUP BY column_name_2;
Examples of Aggregate Functions
For following examples used student table, below is the data from student table.
1) SUM(): Summation of student_roll_no column.
2) MIN(): Minimum number from student_roll_no column.
3) MAX(): Maximum number from student_roll_no column.
4) AVG(): Average of student_roll_no column.
5) COUNT(): Count of student_roll_no column.