PostgreSQL AVG

PostgreSQL AVG

The PostgreSQL AVG aggregate function returns the average in a select query.

AVG syntax

SELECT AVG(table_column)
FROM table_name;

AVG example

Goods table

idgood_typenamedescriptionprice
1ACar_1Car 1 description100
2ACar_2Car 2 description200
3ACar_3Car 3 description100
4BBoat_4Boat 4 description500
5BBoat_5Boat 5 description300
6CTrain_1Train 123 description800
SELECT AVG(price) 
FROM goods;

Result

333.33

SELECT good_type, AVG(price) avg_price
FROM goods
GROUP BY good_type;

Result

good_typeavg_price
B400.00
C800.00
A133.33