Description
The SQL Aggregate Functions can be used to perform calculations on a set of values and return a single value, based on the type of functions.
- These can be used only with a SELECT statement.
- These perform calculations on a set of values and return a single value.
See aggregate functions for more details.
Let's look at an example SELECT statement with all aggregate functions.
Table Date
employee_id | employee_name | gender | birth_date | hire_date | salary |
1 | Robin Hood | M | 1990-10-10 | 2010-10-15 | 25000 |
2 | Tony Blank | M | 1982-08-07 | 2010-01-05 | 89000 |
3 | Andrew Russel | M | 1998-05-04 | 2012-02-20 | 28000 |
4 | James Cooper | NULL | 2000-10-20 | 2015-05-10 | 45000 |
5 | Rose Cutler | F | 1985-08-08 | 2015-06-21 | 65000 |
Example
The below SELECT statement returns the values for all aggregate functions.
SELECT COUNT(*), COUNT(gender), SUM(salary), AVG(salary), MAX(salary), MIN(salary) FROM employees;
After successful execution, the output contains the below data.
COUNT(*) | COUNT(gender) | SUM(salary) | AVG(salary) | MAX(salary) | MIN(salary) |
5 | 4 | 252000 | 50400 | 89000 | 25000 |
Overall
We now understood the usage of all the aggregate functions on a SELECT statement.