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
| id | good_type | name | description | price |
|---|---|---|---|---|
| 1 | A | Car_1 | Car 1 description | 100 |
| 2 | A | Car_2 | Car 2 description | 200 |
| 3 | A | Car_3 | Car 3 description | 100 |
| 4 | B | Boat_4 | Boat 4 description | 500 |
| 5 | B | Boat_5 | Boat 5 description | 300 |
| 6 | C | Train_1 | Train 123 description | 800 |
SELECT AVG(price) FROM goods;
Result
333.33
SELECT good_type, AVG(price) avg_price FROM goods GROUP BY good_type;
Result
| good_type | avg_price |
|---|---|
| B | 400.00 |
| C | 800.00 |
| A | 133.33 |