PostgreSQL Tutorial
Data Types
Querying & Filtering Data
Managing Tables
Modifying Data
Conditionals
Control Flow
Transactions & Constraints
Working with JOINS & Schemas
Roles & Permissions
Working with Sets
Subquery & CTEs
User-defined Functions
Important In-Built Functions
PostgreSQL PL/pgSQL
Variables & Constants
Stored Procedures
Working with Triggers
Working with Views & Indexes
Errors & Exception Handling
In PostgreSQL, the AVG()
function is an aggregate function that returns the average value of a specific column. It's commonly used in conjunction with the GROUP BY
clause to compute the average of groups of rows in the table.
AVG(expression)
Where expression
is typically a column name, but it can also be a result derived from an arithmetic operation or function.
Basic Usage:
Let's say you have a table named students
with columns id
, name
, and score
. To find the average score of all students:
SELECT AVG(score) AS average_score FROM students;
Using GROUP BY
:
If you want to determine the average score of students for each grade level in a grade_level
column:
SELECT grade_level, AVG(score) AS average_score FROM students GROUP BY grade_level;
Combining with Other Aggregate Functions:
To get the minimum, maximum, and average scores:
SELECT MIN(score) AS min_score, MAX(score) AS max_score, AVG(score) AS average_score FROM students;
The AVG()
function works only on numeric, monetary, and interval data types.
If you apply AVG()
to an integer column, the result will be of type numeric
. If you want to get an integer result, you'll need to cast the result accordingly, e.g., CAST(AVG(score) AS INTEGER)
.
NULL
values in the column are ignored when computing the average.
Always be aware of the data's context and distribution when working with averages. The average might not represent the central tendency accurately if there are extreme values or outliers in your data. In such cases, the median or other statistical measures might be more appropriate.
In cases where performance is crucial, and you're dealing with a large dataset, consider indexing or optimizing your database accordingly, especially when combining the AVG()
function with other aggregate functions or complex queries.
Calculating average values in PostgreSQL:
AVG()
to calculate the average of a column.SELECT AVG(column_name) AS average_value FROM your_table;
Using AVG() with GROUP BY in PostgreSQL:
AVG()
with GROUP BY
for calculating group-wise averages.SELECT category, AVG(price) AS avg_price FROM products GROUP BY category;
Handling NULL values with AVG() in PostgreSQL:
AVG()
by excluding them.SELECT AVG(column_name) AS average_value FROM your_table WHERE column_name IS NOT NULL;
Aggregating data with AVG() and HAVING in PostgreSQL:
HAVING
with AVG()
to filter aggregated results.SELECT category, AVG(price) AS avg_price FROM products GROUP BY category HAVING AVG(price) > 50;
Rounding and precision with AVG() in PostgreSQL:
SELECT ROUND(AVG(column_name), 2) AS rounded_average FROM your_table;
AVG() vs other aggregate functions in PostgreSQL:
AVG()
with other aggregate functions like SUM
and COUNT
.SELECT AVG(column_name) AS avg_value, SUM(column_name) AS sum_value, COUNT(column_name) AS count_value FROM your_table;
Using AVG() in window functions in PostgreSQL:
AVG()
in window functions for analytical calculations.SELECT column_name, AVG(column_name) OVER (PARTITION BY partition_column) AS avg_in_partition FROM your_table;