AVG() in SQL
Calculates the arithmetic mean (average) of all non-null values in a specified column or expression.
Syntax
Return type
AVG() Function Example
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
-- Find average product rating
-- Compute average unique price points
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:
COUNT()
Counts the number of rows or non-null values in a specified column. COUNT(*) counts all rows including nulls, while COUNT(column) counts non-null values in the specified column.
SUM()
Calculates the total sum of all values in a specified column or expression, ignoring NULL values.
MIN()
Returns the smallest value from a specified column or expression, ignoring NULL values.
MAX()
Returns the largest value from a specified column or expression, ignoring NULL values.