COUNT
Counts rows or non-NULL values of an expression.
PostgreSQL 17.9
COUNT(* | [DISTINCT] expression)- [ ]optional part
- a | bone of the options
- *
- Count all rows, including rows with NULLKeyword
- DISTINCT
- Count only unique values of expressionKeywordOptional
- 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.9
SELECT COUNT(*)
FROM FamilyMembers;Number of distinct statuses
PostgreSQL 17.9
SELECT COUNT(DISTINCT status)
FROM FamilyMembers;All rows and non-NULL values
PostgreSQL 17.9
SELECT COUNT(*) AS all_teachers,
COUNT(middle_name) AS with_middle_name
FROM Teacher;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 ...).