Function Reference

COUNT

Counts rows or non-NULL values of an expression.

PostgreSQL 17.5
COUNT(* | [DISTINCT] expression)

Parameters

*keyword
Count all rows, including rows with NULL
DISTINCTkeywordoptional
Count only unique values of expression
expression
Expression, only rows where it is not NULL are counted

Return value

An integer (bigint); 0 for an empty set, not NULL

Examples

All rows in the table

PostgreSQL 17.5
SELECT COUNT(*)
FROM FamilyMembers;
count
8

Number of distinct statuses

PostgreSQL 17.5
SELECT COUNT(DISTINCT status)
FROM FamilyMembers;
count
4

All rows and non-NULL values

PostgreSQL 17.5
SELECT COUNT(*) AS all_teachers,
	COUNT(middle_name) AS with_middle_name
FROM Teacher;
all_teacherswith_middle_name
1513

Details

Unlike MySQL, COUNT(DISTINCT a, b) with several columns is not supported: unique pairs are counted with COUNT(DISTINCT (a, b)). To count only the rows that match a condition, use COUNT(*) FILTER (WHERE ...).

See also

Where to learn