SQL Avg
On this page
SQL AVG Function
The AVG() function returns the average (mean) value of a numeric column. It is commonly used for reporting and analysis.
Basic Syntax
SELECT AVG(column_name) FROM table_name;
Example: Average Price
SELECT AVG(price) AS average_price FROM products;
Using AVG with WHERE
You can filter rows before calculating the average.
SELECT AVG(price) AS average_electronics_price FROM products WHERE category = 'Electronics';
Using AVG with GROUP BY
To calculate averages per category or group, combine AVG with GROUP BY.
SELECT category,
AVG(price) AS average_price
FROM products
GROUP BY category
ORDER BY average_price DESC;
Using AVG with Expressions
You can calculate averages based on computed values.
SELECT AVG(price * quantity) AS average_order_value FROM order_items;
Rounding Results
Database systems often allow rounding numeric results for better presentation.
SELECT ROUND(AVG(price), 2) AS rounded_average_price FROM products;
NULL Behavior
AVG() ignores NULL values. If all values are NULL, the result will be NULL.
Common Mistakes
- Using AVG on non-numeric columns
- Forgetting GROUP BY when selecting additional columns
- Not handling NULL values properly
Next Step
Continue with SQL LIKE to learn how to search for patterns in text fields.