aggregate
COUNT
Counts rows with *, nonmissing values with an expression, or distinct nonmissing values with DISTINCT. Empty input gives zero. PostgreSQL returns bigint. The example maps 4 to 3 to demonstrate distinct counting. Example target: PostgreSQL 17; inline synthetic data; no extensions.
Formal Syntax & Structure
sql syntaxsql
COUNT(*) / COUNT(expression) / COUNT(DISTINCT expression)Working Example
Example Snippetsql
WITH sample(id,x,y) AS (VALUES (1,1,2),(2,2,4),(3,3,6),(4,4,8),(5,NULL,4)) SELECT count(*), count(x), count(DISTINCT CASE WHEN x=4 THEN 3 ELSE x END) FROM sample;