EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PostgreSQL Average

Home » Data Science » Data Science Tutorials » PostgreSQL Tutorial » PostgreSQL Average

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 that have aggregate functions that operate on selected rows that will be present in the result set’s target.

Syntax

AVG(column)

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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.

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

distinct values

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. 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 the 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 The amount is taken as a column which needs the average to be found out. Along with 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. 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:

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 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 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 that only CUSTOMER Keys greater than 50 should be considered for taking out the average, then the below query will satisfy 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:

PostgreSQL Average6

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 to 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 an 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 –

  1. PostgreSQL Variables
  2. PostgreSQL Trunc()
  3. PostgreSQL Like
  4. PostgreSQL round

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PostgreSQL Tutorial
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL log_statement
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • 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

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

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

EDUCBA

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

Forgot Password?

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

Special Offer - All in One Data Science Bundle (360+ Courses, 50+ projects) Learn More