EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL Average

PostgreSQL Average

By Aanchal SinghAanchal Singh

PostgreSQL Average

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 to have these rows associated together by some criteria. They can be used with queries with aggregate functions that operate on selected rows present in the result set’s target.

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. This function’s arguments can be of the data type: smallint, int, bigint, real, double precision, numeric, or interval.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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 the 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 that 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 these five students’ average marks, then it can be found out easily by using the AVG() function.

Here the function converts all marks and gives them to the average function. All values in the column are sent to the average function, and the output is a single value, 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 by 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:

charged from a customer

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 using the 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:

distinct values

Explanation: There are many similar entries for the column CYCLE_CODE in the above query. 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. Hence, we use the DISTINCT clause, which helps get distinct values present in the CYCLE_CODE table. After finding the distinct, the AVG() function works on these values and gets the needed average value. 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:

PostgreSQL Average3

Explanation: As explained, this example helps us understand the use of the AVG() function along with a calculation. Here, we have a column called ‘amount’, and we need to find the average value of this column. Along with the average, there is also the need for multiplying this amount by 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. You can group the results and also find the average 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:

PostgreSQL Average4

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 multiple customer keys are having the value of 91, and their collective average is 5.77. The group by clause helps us get the values per the particular conditions. Also, as AVG() is an aggregate function, it has to be 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 the AVG() function.

Code:

SELECT CUSTOMER_KEY, ROUND(avg(AMOUNT),2) AS "Average Amount"
FROM ODS_ABP.BL1_RC_RATES
GROUP BY CUSTOMER_KEY;

Output:

PostgreSQL Average5

Explanation: The HAVING clause can help us further add more conditions to the existing query. If the user wants to consider only ‘CUSTOMER’ keys with a value greater than 50 for calculating the average, then the following query will fulfill this requirement.

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:

PostgreSQL Average6

Explanation: All customer keys greater than 50 are selected.

Conclusion

The AVG() function in PostgreSQL is a very useful aggregate function. You can use it in any field to find the average of multiple rows and produce a single output. We can also use other functionalities of PostgreSQL and enhance its functioning.

Recommended Articles

We hope that this EDUCBA information on “PostgreSQL Average” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. PostgreSQL Variables
  2. Trunc() PostgreSQL
  3. PostgreSQL Export CSV
  4. PostgreSQL round
PROGRAMMING LANGUAGES Course
502+ Hours of HD Videos
54 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SELENIUM Certification Course
57+ Hours of HD Videos
15 Courses
9 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
IOT System - Design & Develop an IOT System
65+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
JENKINS Certification Course
19+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
POSTGRESQL Certification Course
 17+ Hour of HD Videos
4 Courses
1 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more