SQL Aggregate Functions
An aggregate function is a function that performs a calculation on a set of values, and returns a single value.
Aggregate functions are often used with GROUP BY clause of the SELECT statement. The GROUP BY clause splits the
result-set into groups of values and the aggregate function can be used to return a single value for each group.
The most commonly used SQL aggregate functions are:
1) MIN() - return the smallest value within the selected column.
2) MAX() - returns the largest value within the selected column
3) COUNT() - returns the number of rows in a set.
4) SUM() - returns the total sum of a numerical column
5) AVG() - returns the average value of a numerical column.
1) The SQL MIN() and MAX() Functions
The MIN() function returns the smallest value of the selected column.
The MAX() function returns the largest value of the selected column.
• MIN Example Syntax:
SELECT MIN(column_name)
FROM table_name
WHERE condition;
• MAX Example
Syntax:
SELECT MAX(column_name)
FROM table_name
WHERE condition;
Set Column Name
When you use MIN() or MAX(), the returned column will not have a descriptive name. To give the column a descriptive name,
use the AS keyword:
Example
function , to return the smallest price in the Products table:
• Use MIN()
• The SQL COUNT() Function
The COUNT() function returns the number of rows that matches a specified criterion
Syntax
SELECT COUNT(column_name)
FROM table_name
WHERE condition;