Back to Library

AVG() in SQL

Calculates the arithmetic mean (average) of all non-null values in a specified column or expression.

Syntax

AVG([DISTINCT] expression)
Return type
Usually FLOAT/DOUBLE/DECIMAL (may differ from input type)

AVG() Function Example

SELECT AVG(salary) FROM employees; 
-- Output: 65000.00 (average salary)

What is AVG() in SQL?

The AVG() function in SQL calculates the arithmetic mean (average) of all non-null values in a specified column or expression. It is commonly used for financial analysis, performance metrics, and statistical calculations.

This function is supported in SQL Server, MySQL, PostgreSQL, and Oracle.

The AVG() function is widely used in financial reporting, business intelligence, and data analytics.

Parameters:

  • expression: Column name or numeric expression to average

  • DISTINCT (optional): Average only unique values

Example Use Cases:

-- Calculate average order value

SELECT AVG(order_total) AS avg_order_value FROM orders; 
-- Output:
avg_order_value
---------------
    $127.35

-- Find average product rating 

SELECT product_id, AVG(rating) AS avg_rating FROM reviews GROUP BY product_id; 
-- Output:
product_id    avg_rating
----------    ----------
A101            4.2
B205            3.8
C333            4.7
D444            4.1
E555            4.9

-- Compute average unique price points 

SELECT AVG(DISTINCT price) AS avg_unique_price FROM products;
-- Output:
avg_unique_price
----------------
     $75.49

(Calculated from unique prices:
 19.99, 29.99, 49.99, 89.99, 
 99.99, 149.99, 199.99)

Notes:

  • Behavior: Ignores NULL values in calculation

  • Performance Considerations: Division operation may cause rounding

  • Version Info: Core aggregation function available in all major DBMS

  • Deprecated/Recommended Alternatives: None

Error Handling:

  • Error: Returns NULL if all values are NULL

  • Recommendation: Use COALESCE or IF NULL for NULL handling


Supported Databases:

DBMS
Function / Syntax
Example
Behavior with NULL
MySQL

AVG([DISTINCT] expr)

AVG(salary)

Ignores NULL

PostgreSQL

AVG([DISTINCT] expression)

AVG(salary)

Ignores NULL

SQL Server

AVG([DISTINCT] expression)

AVG(salary)

Ignores NULL

SQLite

AVG([DISTINCT] X)

AVG(salary)

Ignores NULL

BigQuery

AVG([DISTINCT] expression)

AVG(salary)

Ignores NULL

Snowflake

AVG([DISTINCT] expression)

AVG(salary)

Ignores NULL

Athena

AVG([DISTINCT] expression)

AVG(salary)

Ignores NULL

Related Functions:

STDEV()

Calculates the standard deviation (population or sample) of a set of numeric values.

Calculates the standard deviation (population or sample) of a set of numeric values.

Calculates the standard deviation (population or sample) of a set of numeric values.

Calculates the standard deviation (population or sample) of a set of numeric values.

VARIANCE()

Calculates the statistical variance (population or sample) of a set of numeric values.

Calculates the statistical variance (population or sample) of a set of numeric values.

Calculates the statistical variance (population or sample) of a set of numeric values.

Calculates the statistical variance (population or sample) of a set of numeric values.

STRING_AGG()

Concatenates values from multiple rows into a single string, with specified delimiter.

Concatenates values from multiple rows into a single string, with specified delimiter.

Concatenates values from multiple rows into a single string, with specified delimiter.

Concatenates values from multiple rows into a single string, with specified delimiter.

GROUP_CONCAT()

Concatenates values from multiple rows into a single string, with optional delimiter and ordering.

Concatenates values from multiple rows into a single string, with optional delimiter and ordering.

Concatenates values from multiple rows into a single string, with optional delimiter and ordering.

Concatenates values from multiple rows into a single string, with optional delimiter and ordering.

AVG()

Calculates the arithmetic mean (average) of all non-null values in a specified column or expression.

Calculates the arithmetic mean (average) of all non-null values in a specified column or expression.

Calculates the arithmetic mean (average) of all non-null values in a specified column or expression.

Calculates the arithmetic mean (average) of all non-null values in a specified column or expression.

One place for all your queries,
directly on your SQL editor