Postgresql Count Function

PostgreSQL: count Function

In this PostgreSQL post explains how to use the PostgreSQL count function with syntax and examples.

Description

The PostgreSQL count function returns the count of an expression.

Syntax

The syntax for the count function in PostgreSQL is:

SELECT count(aggregate_expression)
FROM tables
[WHERE conditions];

OR the syntax for the count function when grouping the results by one or more columns is:

SELECT expression1, expression2, ... expression_n,
       count(aggregate_expression)
FROM tables
[WHERE conditions]
GROUP BY expression1, expression2, ... expression_n;

Parameters or Arguments

expression1, expression2, ... expression_n

Expressions that are not encapsulated within the count function and must be included in the GROUP BY clause at the end of the SQL statement.

aggregate_expression

This is the column or expression whose non-null values will be counted.

tables

The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.

WHERE conditions

Optional. These are conditions that must be met for the records to be selected.

Only includes NOT NULL Values

Not everyone realizes this, but the count function will only include the records in the count where the value of expression in count(expression) is NOT NULL. When expression contains a NULL value, it is not included in the count calculations.

Let's look at a count function example that demonstrates how NULL values are evaluated by the count function.

For example, if you have the following table called suppliers:

supplier_id supplier_name state
1 IBM CA
2 Microsoft
3 NVIDIA

And if you ran the following SELECT statement that uses the count function:

SELECT count(supplier_id)
FROM suppliers;

Output: 3

This count example will return 3 since all supplier_id values in the query's result set are NOT NULL.

However, if you ran the next SELECT statement that uses the count function:

SELECT count(state) 
FROM suppliers;

Output: 1

This count example will only return 1, since only one state value in the query's result set is NOT NULL. That would be the first row where the state = 'CA'. It is the only row that is included in the count function calculation.

Applies To

The count function can be used in the following versions of PostgreSQL:

  • PostgreSQL 9.4, PostgreSQL 9.3, PostgreSQL 9.2, PostgreSQL 9.1, PostgreSQL 9.0, PostgreSQL 8.4

Example - With Single Expression

Let's look at some PostgreSQL count function examples and explore how to use the count function in PostgreSQL.

For example, you might wish to know the number of products that have the product_type of 'Hardware'.

SELECT count(*) AS "Number of products"
FROM products
WHERE product_type = 'Hardware';

In this count function example, we've aliased the count(*) expression as "Number of products". As a result, "Number of products" will display as the field name when the result set is returned.

Example - Using DISTINCT

You can use the DISTINCT clause within the count function. For example, the SQL statement below returns the number of unique departments where at least one employee makes over $55,000 / year.

SELECT count(DISTINCT department) AS "Unique departments"
FROM employees
WHERE salary > 55000;

Again, the count(DISTINCT department) field is aliased as "Unique departments". This is the field name that will display in the result set.

Example - Using GROUP BY

In some cases, you will be required to use the GROUP BY clause with the count function.

For example, you could also use the count function to return the name of the department and the number of employees (in the associated department) that make over $40,000.

SELECT department, count(*) AS "Number of employees"
FROM employees
WHERE salary > 40000
GROUP BY department;

Because you have listed one column in your SELECT statement that is not encapsulated in the count function, you must use a GROUP BY clause. The department field must, therefore, be listed in the GROUP BY section.