Aggregate_functions_in_SQL.md - brainchildservices/curriculum GitHub Wiki
## Aggregate functions in SQL
In database management, an aggregate function is a function where the values of multiple rows are grouped together as input on certain criteria to form a single value of more significant meaning.
An aggregate function performs a calculation on a set of values and returns a single value. Except for COUNT(*), aggregate functions ignore null values. Aggregate functions are often used with the GROUP BY clause of the SELECT statement.
## Various Aggregate Functions
-
AVG – calculates the average of a set of values.
-
COUNT – counts rows in a specified table or view.
-
MIN – gets the minimum value in a set of values.
-
MAX – gets the maximum value in a set of values.
-
SUM – calculates the sum of values.
Notice that all aggregate functions above ignore NULL values except for the COUNT function.
SQL aggregate functions syntax
aggregate_function (DISTINCT | ALL expression)
AVG() Aggregate Function
The AVG() aggregate function returns the average value in a column. For instance, to find the average salary for the employees who have less than 5 years of experience, the given query can be used.
SELECT AVG(salary)
FROM employees**
WHERE experience < 5;
COUNT() Aggregate Function
The COUNT() aggregate function returns the total number of rows that match the specified criteria. For instance, to find the total number of employees who have less than 5 years of experience, the given query can be used.
SELECT COUNT(*)
FROM employees
WHERE experience < 5;
MIN() Aggregate Function
The MIN() aggregate function returns the smallest value in a column. For instance, to find the smallest value of the amount column from the table named transactions, the given query can be used.
SELECT MIN(amount)
FROM transactions;
MAX() Aggregate Function
The MAX() aggregate function takes the name of a column as an argument and returns the largest value in a column. The given query will return the largest value from the amount column.
SELECT MAX(amount)
FROM transactions;
SUM() Aggregate Function
The SUM() aggregate function takes the name of a column as an argument and returns the sum of all the values in that column.
SELECT SUM(salary)
FROM salary_disbursement;