Aggregate Functions
Aggregate functions are single values computed from several rows of data.
These functions include MAX
, MIN
, AVG
, ROUND
and SUM
.
Check out the PostgreSQL’s full list of aggregate functions.
Common aggregate functions
Function | Description |
---|---|
Max | Computes the max of non-null input values. |
Min | Computes the min of non-null input values. |
Avg | Computes the average (arithmetic mean) of all the non-null input values. |
Round | Rounds of all the non-null input values. Round can also specify the amount of decimal places with a second argument. |
Sum | Computes the sum of the non-null input values. |
Syntax
SELECT <aggregate-function>(<column>) FROM <table>;
Example #1
SELECT MAX(founded) FROM Companies;
Max |
---|
1998 |
Note: In some cases, an aggregate function can be used within another.
For example:ROUND(AVG(price), 2)
, rounds the average value ofprice
up to two decimal places.
Example #2
SELECT hq, MIN(founded) FROM Companies
GROUP BY hq;
HQ | Min |
---|---|
United States | 1923 |
Canada | 1984 |