PostgreSQL 18 reference · Aggregate Functions
PostgreSQL COUNT() Function
Count rows: count(*) counts every row, count(x) skips NULLs, and count(DISTINCT x) counts unique values. Returns bigint.
PostgreSQL 18
Syntax
count(*) | count(expression) | count(DISTINCT expression)
Parameters
| Parameter | Description |
|---|---|
expression |
A column or expression. |
Return value
bigint (0 for no rows).
Examples
SELECT count(*) AS customers, count(email) AS with_email, count(DISTINCT country) AS countries
FROM customers;
Output
customers | with_email | countries
-----------+------------+-----------
10 | 9 | 5
(1 row)Run this on your computer — see “Get Started” for setup.
SELECT status, count(*) AS orders
FROM orders
GROUP BY status
ORDER BY orders DESC, status;
Output
status | orders -----------+-------- delivered | 7 pending | 2 shipped | 2 cancelled | 1 (4 rows)
Run this on your computer — see “Get Started” for setup.
In other languages
COUNT()· SQLite
Related
SUM()· Add up the non-NULL values in a group. Returns NULL, not 0, when there are no rows; wrap it in coalesce() if needed.FILTER· Restrict which rows one aggregate sees with FILTER (WHERE …), so one query can count several conditions.- SQL Aggregate Functions · tutorial
- SQL GROUP BY · tutorial