SQL AVG Function
Quick summary: The SQL AVG() function returns the average value of a numeric column.
SQL AVG Syntax
SELECT AVG(column_name) FROM table_name WHERE condition
SQL
SQL AVG Basic examples
SELECT AVG(price) FROM products;
SQL
Output:
Average product price
Calculates the mean value.
SQL AVG Real-world usage
SELECT AVG(total) FROM orders WHERE status = 'paid';
SQL
Output:
Average order value
Used for analytics.
SQL AVG Edge cases
SELECT AVG(price) FROM products WHERE category_id = 999;
SQL
Output:
NULL
Returns NULL when no rows match.
SQL AVG Common mistakes
Averaging integers without casting
Some databases may return integer division.
Incorrect
AVG(quantity)
Correct
AVG(quantity * 1.0)
Ensure floating-point arithmetic.
SQL AVG Frequently Asked Questions
What does AVG() do in SQL?
Calculates the average value of a column.
Use case of AVG()?
Mean calculations.
Handles NULL?
Ignores NULL values.
Return type?
Numeric (float).
Common mistake?
Expecting integer result.
Works with GROUP BY?
Yes.
Supports DISTINCT?
Yes.
Performance?
Efficient.
Used in analytics?
Yes.
Alternative?
SUM()/COUNT().
Handles decimals?
Yes.
Best practice?
Check precision.