## Introduction to PostgreSQL Average

The average function is a function that can work on multiple rows at the same time. The result of the average function is a single result. The main functioning is finding the average value of a particular numeric column. It is usually used with the GROUP BY clause in order to have these rows associated together by some criteria. They can be used with queries that have aggregate functions that operate on selected rows that will be present in the target of the result set.

### Syntax

`AVG(column)`

**Explanation:** This function can be used to select and having clauses. It will find the average or arithmetic mean of all input values which are provided. The arguments for this function can be of the data type: smallint, int, bigint, real, double precision, numeric, or interval.

**Result:** The result for this function is numeric for any integer type of argument. It is double for any floating argument or else the same data type as the argument. These can also be grouped when Having clause is used in the query.

### How Average function works in PostgreSQL?

Let us have a look at how the average function works.

Let us consider a column which has the following values:

MARKS

——-

23

78

87

56

45

The PostgreSQL AVG() function will help us in getting the average of this column Marks which has different marks. If the teacher of this class wants to find the average marks of these five students then it can be found out easily by using the AVG() function.

Here the function converts all marks and gives it to the average function. All values in the column are sent to the average function and the output is a single value which is the average of these columns.

`AVG(MARKS)`

MARKS(23, 78, 87, 56, 45)

The output of the above AVG() function will hence be 57.8

`AVG()`

At the back end the AVG() function takes the addition of all these five numbers and then divides the total by the number of marks. To explain this it does:

23 + 78 + 87 + 56 + 45 = 289

289 / 5 = 57.8

The output of the above AVG() function will hence be 57.8

### Examples to Implement PostgreSQL Average

Let’s look at some PostgreSQL avg function examples and explore how to use the avg function :

#### Example #1

It may happen that a company would like to know the average amount charged from a customer. The below query can hence help in getting this value.

**Code:**

`SELECT AVG(CHARGED_AMOUNT) AS "Average Charged Amount"`

FROM APP_IPTV.VOD_PAID_TRX_EVENT_STG;

**Output:**

**Explanation:** The amount’s average value is found by making use of this query. The charged amount’s average can be found and can be used further.

#### Example #2

There can be cases where there are duplicates present in your table for a particular column. You would like to get the average but you want the average to be of distinct values present in the table. This can also be done in PostgreSql by making use of AVG() function and the DISTINCT clause in the query.

**Code:**

`SELECT avg(DISTINCT CYCLE_CODE) AS "Average Cycle Code"`

FROM APP_IPTV.VOD_PAID_RECON

WHERE TRX_TYPE = 'EVENT';

**Output:**

**Explanation: **In the above query, there are many similar entries for the column CYCLE_CODE. It may be needed that we require that for a filter condition like in the above query, TRX_TYPE = ‘EVENT’ and there are multiple CYCLE CODEs for this filter. We hence use the DISTINCT clause which helps in getting distinct values in present in the CYCLE_CODE table. After finding the distinct the AVG() function works on these values and gets the average value that is needed. Above is the output which you will get of the query with the DISTINCT clause.

#### Example #3

After finding the distinct let us see how to make use of AVG() with a formula or a mathematical calculation. The AVG() function can also be used along with any formula. The below code with help you understand this better.

**Code:**

`SELECT avg(AMOUNT * 10) AS "Average Amount"`

FROM ODS_ABP.BL1_RC_RATES;

**Output:**

**Explanation: **As explained this example helps us understand the use of AVG() function along with a calculation. Here The amount is taken as a column which needs the average to be found out. Along with average there is also the need of multiplying this amount with 10. The above query does this job. It first finds the average and then multiplies it with 10. The output is the value of this calculation.

#### Example #4

The AVG() function in PostgreSQL helps in grouping by the results of average as per some field. The results can be grouped by and average can also be found at the same time in the same query. The below code will help you understand this better.

**Code:**

`SELECT CUSTOMER_KEY, avg(AMOUNT) AS "Average Amount"`

FROM ODS_ABP.BL1_RC_RATES

GROUP BY CUSTOMER_KEY;

**Output:**

**Explanation: **The above query is finding all customer keys along with the corresponding average amounts for the customer key groups. The customer key 91 has an average of 5.77. This means there are multiple customer keys having the value 91 and their collective average is 5.77. The group by clause helps us in getting the values as per the particular conditions. Also as AVG() is an aggregate function it has to accompanied by a group by clause. To improvise this and get the average result only up to two decimal places the ROUND() function can also be used along with AVG(). The below query helps us in rounding off the values of AVG() function.

**Code:**

`SELECT CUSTOMER_KEY, ROUND(avg(AMOUNT),2) AS "Average Amount"`

FROM ODS_ABP.BL1_RC_RATES

GROUP BY CUSTOMER_KEY;

**Output:**

**Explanation:** The HAVING clause can help us further add more conditions to the existing query. If the user wants that only CUSTOMER Keys which are greater than 50 should be considered for taking out the average then the below query with satisfying this need.

**Code:**

`SELECT CUSTOMER_KEY, ROUND(avg(AMOUNT),2) AS "Average Amount"`

FROM ODS_ABP.BL1_RC_RATES

GROUP BY CUSTOMER_KEY

Having CUSTOMER_KEY > 50;

**Output:**

**Explanation:** All customer keys greater than 50 are selected.

### Conclusion

The AVG() function in PostgreSQL is hence a very useful aggregate function. It can be used in all fields as it can find an average of multiple rows and give a single output. Along with this we can use other functionalities of PostgreSQL and enhance its functioning.

### Recommended Articles

This is a guide to PostgreSQL Average. Here we discuss introduction to PostgreSQL Average, syntax, how does the average function work with respective query examples. You can also go through our other related articles to learn more –