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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

count(*)  |  count(expression)  |  count(DISTINCT expression)

Parameters

ParameterDescription
expression A column or expression.

Return value

bigint (0 for no rows).

Examples

Three kinds of count
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.

Per group
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

Was this page helpful?