PostgreSQL 18 reference · Aggregate Functions
PostgreSQL AVG() Function
Return the average of the non-NULL values in a group. Integer input gives a numeric result with full precision.
PostgreSQL 18
Syntax
avg(expression)
Parameters
| Parameter | Description |
|---|---|
expression |
A column or expression. |
Return value
numeric (or double precision for float input), or NULL.
Examples
SELECT department, round(avg(salary)) AS avg_salary
FROM employees
GROUP BY department
ORDER BY avg_salary DESC;
Output
department | avg_salary -------------+------------ Management | 250000 Engineering | 148400 Sales | 109333 Support | 65000 (4 rows)
Run this on your computer — see “Get Started” for setup.
SELECT avg(stock) AS raw_average FROM products;
Output
raw_average --------------------- 41.2500000000000000 (1 row)
Run this on your computer — see “Get Started” for setup.
In other languages
AVG()· SQLite
Related
SUM()· Add up the non-NULL values in a group. Returns NULL, not 0, when there are no rows; wrap it in coalesce() if needed.ROUND()· Round a number to a given number of decimal places (0 by default). numeric rounds halves away from zero.PERCENTILE_CONT()· Return the value at a percentile, interpolating between rows: percentile_cont(0.5) is the median.- SQL Aggregate Functions · tutorial
- SQL GROUP BY · tutorial