Contents / Previous / Next


Aggregate Functions

An aggregate function computes a single result from multiple input rows.
Five popular aggregate functions are:
Aggregate Function
COUNT(*) count of rows
SUM(colname) total
MAX(colname) maximum
MIN(colname) minimum
AVG(colname) average


For example, count the students and calculate the average semester:

> SELECT count(semester) FROM stud ; count ------- 4 (1 row) > SELECT avg(semester) FROM stud; avg -------------- 2.0000000000 (1 row) NULL values are not processed by most aggregates, such as MAX(), SUM(), and AVG(); they are simply ignored.
COUNT(*) is different in this respect. It does count NULL values because it looks at entire rows using the asterisk(*).