How to Use AVG in SQL
Use AVG() when you need the average value of a numeric column across matching rows. This is ideal for pricing analysis, ratings, response times, and performance dashboards.
What you’ll build or solve
You’ll learn how to use AVG in SQL for overall averages, filtered averages, and grouped averages. You’ll also know how NULL values affect the result.
Learn SQL on Mimo
When this approach works best
This approach is the right choice when the mean value matters more than the full row list.
Common real-world scenarios include:
- Average order value
- Product ratings
- Response time monitoring
- Exam scores
- Average session duration
This is a bad idea when median or percentile values are more meaningful than the mean.
Prerequisites
You only need:
- A SQL table with numeric columns
- Basic
SELECT,WHERE, andGROUP BYknowledge
Step-by-step instructions
Step 1: Wrap the numeric column in AVG()
Use AVG() around the numeric field.
SQL
SELECT AVG(total)
FROM orders;
This returns the mean value across all rows.
Add filters before averaging.
SQL
SELECT AVG(total)
FROM orders
WHERE status = 'paid';
This is especially useful for average order value.
Combine it with GROUP BY for segmented averages.
SQL
SELECT country, AVG(total)
FROM orders
GROUP BY country;
What to look for:
AVG()calculates the meanWHEREfilters before averagingGROUP BYcreates segmented averagesNULLvalues are ignored- Great for ratings and revenue analysis
Examples you can copy
Average rating
SQL
SELECT AVG(rating)
FROM reviews;
Average paid order
SQL
SELECT AVG(total)
FROM orders
WHERE status = 'paid';
Average by country
SQL
SELECT country, AVG(total)
FROM orders
GROUP BY country;
Common mistakes and how to fix them
Mistake 1: Averaging text columns
What the reader might do:
SQL
SELECT AVG(status)
FROM orders;
Why it breaks: text cannot be averaged.
Corrected approach:
Use only numeric columns.
Mistake 2: Forgetting GROUP BY
What the reader might do:
SQL
SELECT country, AVG(total)
FROM orders;
Why it breaks: SQL needs grouping for non-aggregated columns.
Corrected approach:
SQL
GROUP BY country
Mistake 3: Misreading ignored NULL values
What the reader might do:
SQL
SELECT AVG(discount)
FROM orders;
Why it breaks: rows with NULL discounts are skipped.
Corrected approach:
Use COALESCE(discount, 0) if missing values should count as zero.
Troubleshooting
If the average seems too high, check whether NULL rows were skipped.
If the query errors, add GROUP BY.
If the result is NULL, confirm matching rows exist.
If zero fallback is required, use COALESCE().
Quick recap
- Use
AVG()for numeric averages - Add
WHEREfor filtered averages - Use
GROUP BYfor segmented averages NULLvalues are ignored- Use
COALESCE()when zero fallback is needed
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot