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.

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, and GROUP BY knowledge

Step-by-step instructions

Step 1: Wrap the numeric column in AVG()

Use AVG() around the numeric field.

SELECT AVG(total)
FROM orders;

This returns the mean value across all rows.

Add filters before averaging.

SELECT AVG(total)
FROM orders
WHERE status = 'paid';

This is especially useful for average order value.

Combine it with GROUP BY for segmented averages.

SELECT country, AVG(total)
FROM orders
GROUP BY country;

What to look for:

  • AVG() calculates the mean
  • WHERE filters before averaging
  • GROUP BY creates segmented averages
  • NULL values are ignored
  • Great for ratings and revenue analysis

Examples you can copy

Average rating

SELECT AVG(rating)
FROM reviews;

Average paid order

SELECT AVG(total)
FROM orders
WHERE status = 'paid';

Average by country

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:

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:

SELECT country, AVG(total)
FROM orders;

Why it breaks: SQL needs grouping for non-aggregated columns.

Corrected approach:

GROUP BY country

Mistake 3: Misreading ignored NULL values

What the reader might do:

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 WHERE for filtered averages
  • Use GROUP BY for segmented averages
  • NULL values are ignored
  • Use COALESCE() when zero fallback is needed