PostgreSQL Group By

PostgreSQL Group By

The PostgreSQL GROUP BY is used when at least one of the aggregation functions(count, max, min, sum, avg) is used in a select.

Group By syntax

SELECT column_name1, column_name2, function(column_name1)
FROM table_name
GROUP BY column_name1, column_name2

Group By 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 good_type, count(good_type) g_count
FROM goods
GROUP BY good_type

Result

good_typeg_count
B2
C1
A3