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;Number of distinct statuses
PostgreSQL 17.5
SELECT COUNT(DISTINCT status)
FROM FamilyMembers;All rows and non-NULL values
PostgreSQL 17.5
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 ...).