aggregate

COUNT

Returns the number of rows matching a query, optionally counting non-NULL values of an expression or distinct values. COUNT(*) counts every row including NULLs, while COUNT(expr) skips NULLs.

COUNT(* | [DISTINCT] expression)

Parameters / Modifiers

Parameter Purpose
* Count every row regardless of NULLs (fastest for row totals)
DISTINCT expr Count unique non-NULL values of expr
expr Count non-NULL values of a specific column or expression
FILTER (WHERE ...) Postgres: conditional counting inside aggregate

Examples

SELECT COUNT(*) FROM orders;

Total number of order rows including any with NULL columns

SELECT COUNT(DISTINCT customer_id) FROM orders;

Number of unique customers who placed orders

SELECT COUNT(shipped_at) FROM orders;

Only rows where shipped_at IS NOT NULL

SELECT COUNT(*) FILTER (WHERE status = 'paid') FROM orders;

Postgres-only conditional count without a WHERE clause

Dialect notes / Gotcha

COUNT(column) silently ignores NULLs — use COUNT(*) for a true row count. FILTER clause is Postgres/SQLite 3.30+; MySQL requires COUNT(CASE WHEN ... THEN 1 END).

Related functions

← All SQL functions · SQL cheat sheet · Window functions · PostgreSQL cheat sheet