PostgreSQL Count

PostgreSQL Count

The PostgreSQL COUNT function returns the number of the rows in a select.

COUNT syntax

SELECT COUNT(*) FROM table_name;

SELECT COUNT(column_name) FROM table_name;

COUNT 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 COUNT(*) 
FROM goods;

Result

6

SELECT COUNT(price) 
FROM goods 
WHERE price > 200;

Result

3

SELECT good_type, COUNT(price) n_count
FROM goods
GROUP BY good_type
HAVING count(good_type) > 1;

Result

good_typen_count
B2
A3