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

PostgreSQL - AVG() Function

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.

Syntax:

AVG(expression)

Where expression is typically a column name, but it can also be a result derived from an arithmetic operation or function.

Examples:

  1. 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;
    
  2. 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;
    
  3. 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;
    

Points to Note:

  • 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.

  1. Calculating average values in PostgreSQL:

    • Description: Basic usage of AVG() to calculate the average of a column.
    • Code Example:
      SELECT AVG(column_name) AS average_value
      FROM your_table;
      
  2. Using AVG() with GROUP BY in PostgreSQL:

    • Description: Applying AVG() with GROUP BY for calculating group-wise averages.
    • Code Example:
      SELECT category, AVG(price) AS avg_price
      FROM products
      GROUP BY category;
      
  3. Handling NULL values with AVG() in PostgreSQL:

    • Description: Managing NULL values when using AVG() by excluding them.
    • Code Example:
      SELECT AVG(column_name) AS average_value
      FROM your_table
      WHERE column_name IS NOT NULL;
      
  4. Aggregating data with AVG() and HAVING in PostgreSQL:

    • Description: Using HAVING with AVG() to filter aggregated results.
    • Code Example:
      SELECT category, AVG(price) AS avg_price
      FROM products
      GROUP BY category
      HAVING AVG(price) > 50;
      
  5. Rounding and precision with AVG() in PostgreSQL:

    • Description: Controlling rounding and precision of the average result.
    • Code Example:
      SELECT ROUND(AVG(column_name), 2) AS rounded_average
      FROM your_table;
      
  6. AVG() vs other aggregate functions in PostgreSQL:

    • Description: Comparing AVG() with other aggregate functions like SUM and COUNT.
    • Code Example:
      SELECT AVG(column_name) AS avg_value,
             SUM(column_name) AS sum_value,
             COUNT(column_name) AS count_value
      FROM your_table;
      
  7. Using AVG() in window functions in PostgreSQL:

    • Description: Applying AVG() in window functions for analytical calculations.
    • Code Example:
      SELECT column_name, AVG(column_name) OVER (PARTITION BY partition_column) AS avg_in_partition
      FROM your_table;