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

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 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_type n_count
B 2
A 3