PostgreSQL COUNT Function
Summary: in this tutorial, you will learn how to use the PostgreSQL COUNT()
function to count the number of rows in a table.
Introduction to PostgreSQL COUNT() function
The COUNT()
function is an aggregate function that allows you to obtain the number of rows that match a specific condition.
The following statement illustrates various ways of using the COUNT()
function.
COUNT(*)
The COUNT(*)
function returns the number of rows returned by a SELECT
statement, including NULL and duplicates.
When you apply the COUNT(*)
function to the entire table, PostgreSQL has to scan the whole table sequentially. If you use the COUNT(*)
function on a big table, the query will be slow. This is related to the PostgreSQL MVCC implementation.
Due to multiple transactions seeing different states of data simultaneously, there is no direct way for COUNT(*)
function to count across the entire table. Therefore, PostgreSQL must scan all rows.
COUNT(column)
Similar to the COUNT(*)
function, the COUNT(column_name)
function returns the number of rows returned by a SELECT
clause. However, it does not consider NULL
values in the column_name
.
COUNT(DISTINCT column)
In this syntax, the COUNT(DISTINCT column_name)
returns the number of unique non-null values in the column_name
.
In practice, you often use the COUNT()
function with the GROUP BY
clause to return the number of items for each group.
For example, you can use the COUNT()
with the GROUP BY
clause to return the number of films in each film category.
PostgreSQL COUNT() function examples
Let’s use the payment
table in the sample database for the demonstration.
1) Basic PostgreSQL COUNT(*) example
The following statement uses the COUNT(*)
function to return the number of transactions in the payment
table:
Output:
2) PostgreSQL COUNT(DISTINCT column) example
To get the distinct amounts that customers paid, you use the COUNT(DISTINCT amount)
function as shown in the following example:
Output:
3) Using PostgreSQL COUNT() function with GROUP BY clause example
The following example uses the COUNT()
function with the GROUP BY
function to return the number of payments of each customer:
Output:
If you want to display the customer name instead of id, you can join the payment table with the customer table:
Output:
4) Using PostgreSQL COUNT() function with HAVING clause
You can use the COUNT
function in a HAVING
clause to apply a specific condition to groups. For example, the following statement finds customers who have made over 40 payments:
Output:
Summary
- Use the PostgreSQL
COUNT()
function to return the number of rows in a table.