PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
    • Aggregate Functions
    • Date / Time Functions
    • String Functions
    • Math Functions
Home / PostgreSQL Aggregate Functions / PostgreSQL AVG Function

PostgreSQL AVG Function

Summary: in this tutorial, you will learn how to use PostgreSQL AVG function to calculate average value of a set.

Introduction to PostgreSQL AVG function

The AVG function is one of the most commonly used aggregate functions in PostgreSQL. The AVG() function allows you to calculate the average value of a numeric column.

The syntax of the AVG function is as follows:

1
AVG(column)

You can use the AVG() function in the SELECT and HAVINGclauses. Let’s take a look at some examples of using

Let’s take a look at some examples of using the AVG function.

We will use the following  payment table in the dvdrental sample database for demonstration:

payment table

PostgreSQL AVG function examples

If you want to know average amount that customers paid, you can apply the AVG function on the  amount column as the following query:

1
2
3
4
5
6
7
SELECT
to_char(
AVG (amount),
'99999999999999999D99'
) AS average_amount
FROM
payment;

postgresql avg function example

Note that we used to_char() function to convert the result into a formatted string.

PostgreSQL AVG function with DISTINCT

To calculate average value of a set with the condition that only distinct values are taken into calculation, you use DISTINCT as follows:

1
AVG(DISTINCT column)

For example, the following query returns the average payment made by customers. Because we use DISTINCT, PostgreSQL only takes unique amounts and calculates the average.

1
2
3
4
5
6
7
SELECT
TO_CHAR(
AVG (DISTINCT amount),
'FM999999999.00'
)
FROM
payment;

postgresql avg distinct example

Notice that the result is different from the first example.

PostgreSQL AVG function with SUM function

The following query uses both SUM function and AVG function to calculate the total payment made by customers and average of all transactions.

1
2
3
4
5
6
7
8
9
10
11
SELECT
TO_CHAR(
AVG (amount),
'FM999999999.00'
) AS "Average",
TO_CHAR(
SUM (amount),
'FM999999999.00'
) AS "Total"
FROM
payment;

PostgreSQL AVG with SUM function

PostgreSQL AVG function with GROUP BY clause

To calculate the average value of a group, you use the AVG function with GROUP BY clause. First, the GROUP BY clause divides rows of the table into groups, the AVG function is then applied for each group.

For example, to calculate average amount paid by each customer, you use the following query:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
SELECT
customer.customer_id,
first_name,
last_name,
to_char(
AVG (amount),
'99999999999999999D99'
) AS average_amount
FROM
payment
INNER JOIN customer ON customer.customer_id = payment.customer_id
GROUP BY
customer.customer_id
ORDER BY
customer_id;

postgresql avg function with GROUP BY

In the query, we joined the payment table with the customer table using inner join. We used GROUP BY clause to group customers into groups and applied the AVG function to calculate the average per group.

The following diagram illustrates how PostgreSQL performs the query.

PostgreSQL AVG function with GROUP BY query explain

PostgreSQL AVG function with HAVING clause

You can use the AVG function in the HAVING clause to filter the group based on a certain condition. For example, for all customers, you can get the customers who paid the average payment bigger than 5 USD. The following query helps you to do so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
SELECT
customer.customer_id,
first_name,
last_name,
to_char(
AVG (amount),
'99999999999999999D99'
) AS average_amount
FROM
payment
INNER JOIN customer ON customer.customer_id = payment.customer_id
GROUP BY
customer.customer_id
HAVING
AVG (amount) > 5
ORDER BY
customer_id;

postgresql avg function with HAVING

This query is similar to the one above with an additional HAVING clause. We used AVG function in the HAVING clause to filter the groups that have average amount less than or equal to 5.

PostgreSQL AVG function and NULL values

How the AVG function treats null values when it calculates the average? Let’s test it.

First, we create a table named t1.

1
2
3
4
CREATE TABLE t1 (
ID serial PRIMARY KEY,
amount INTEGER
);

Second, we insert some sample data:

1
2
3
4
5
INSERT INTO t1 (amount)
VALUES
(10),
(NULL),
(30);

The data of the t1 table is as follows:

1
2
3
4
SELECT
*
FROM
t1;

postgresql avg with null

Third, we use the AVG function to calculate average values in the amount column.

1
2
3
4
SELECT
AVG (amount)
FROM
t1;

postgresql avg with null example

We got 20. It means that the AVG function ignore NULL values when it calculates the average.

In this tutorial, we have shown you various examples using the PostgreSQL AVG function to calculate average value of a set.

Related Tutorials

  • PostgreSQL MAX Function
  • PostgreSQL MIN Function
  • PostgreSQL SUM Function
  • PostgreSQL COUNT Function
Next Tutorial: PostgreSQL COUNT Function

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Aggregate Functions

  • PostgreSQL COUNT Function
  • PostgreSQL AVG Function
  • PostgreSQL MAX Function
  • PostgreSQL MIN Function
  • PostgreSQL SUM Function

PostgreSQL Conditional Expressions

  • PostgreSQL CASE
  • PostgreSQL NULLIF
  • PostgreSQL COALESCE

PostgreSQL String functions

  • PostgreSQL CONCAT Function
  • PostgreSQL TRIM Function
  • PostgreSQL LENGTH Function
  • PostgreSQL REPLACE
  • PostgreSQL Substring
  • PostgreSQL Letter Case Functions

PostgreSQL Operators

  • PostgreSQL CAST

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • PostgreSQL ROUND
  • PostgreSQL Math Functions
  • PostgreSQL LOCALTIME
  • PostgreSQL LOCALTIMESTAMP
  • PostgreSQL CURRENT_TIMESTAMP
  • PostgreSQL CURRENT_TIME
  • PostgreSQL CURRENT_DATE
  • PostgreSQL Date Functions
  • PostgreSQL EXTRACT
  • PostgreSQL String Functions

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.