CGPA Calculator Hash Generator Bcrypt Hasher Tree Visualizer Barcode Generator Engineering Blog Editorial Standards All Tools Games HTML5 JavaScript PHP MySQL

MySQL AVG() Function

The AVG() function calculates the arithmetic mean of all non-null values in a numeric column. It divides the total sum by the count of non-null rows automatically. Use it whenever you need to find the average price, score, salary, rating, or any other numeric measure across a set of rows.


Syntax

SELECT AVG(column_name) AS alias FROM table_name;

Sample Table

idnamecategorypricequantity_sold
1LaptopElectronics55000.005
2PhoneElectronics25000.0012
3T-ShirtClothing800.0040
4HeadphonesElectronics3500.0018
5JeansClothing2200.0022
6TabletElectronics35000.008
7JacketClothing4500.0015

Basic AVG()

Calculate the average price across all products:

SELECT AVG(price) AS avg_price FROM products;
-- Returns: 18000.000000

MySQL often returns many decimal places. Use ROUND() to make the output cleaner:

SELECT ROUND(AVG(price), 2) AS avg_price FROM products;
-- Returns: 18000.00

AVG() with WHERE

Calculate the average for a filtered subset:

-- Average price of Electronics products only
SELECT ROUND(AVG(price), 2) AS avg_electronics_price
FROM products
WHERE category = 'Electronics';
-- Returns: 29625.00

-- Average quantity sold for products priced below 5000
SELECT ROUND(AVG(quantity_sold), 1) AS avg_sold_cheap
FROM products
WHERE price < 5000;
-- Returns: 25.7

AVG() with GROUP BY

Get the average price per category:

SELECT category, ROUND(AVG(price), 2) AS avg_price
FROM products
GROUP BY category;
categoryavg_price
Electronics29625.00
Clothing2500.00

AVG() and NULL Values

Like all aggregate functions, AVG() ignores NULL values. The average is calculated only from the rows that have a real value — NULL rows are excluded from both the sum and the count.

-- If quantity_sold were NULL for one row, it is excluded:
-- AVG is calculated as: SUM(non-null values) / COUNT(non-null rows)
Important: This means AVG() does not treat NULL as zero. If you want nulls counted as zero in the average, replace them first: AVG(COALESCE(column, 0)).
-- Treat NULL quantity_sold as 0 in the average
SELECT ROUND(AVG(COALESCE(quantity_sold, 0)), 2) AS avg_with_nulls_as_zero
FROM products;

Comparing AVG with MIN and MAX

Often useful to see all three together for a quick statistical summary:

SELECT
    ROUND(AVG(price), 2) AS avg_price,
    MIN(price)           AS min_price,
    MAX(price)           AS max_price
FROM products;
avg_pricemin_pricemax_price
18000.00800.0055000.00

Key Points to Remember

  • AVG() returns the mean of all non-null values in a column.
  • Use ROUND(AVG(column), n) to control the number of decimal places in the result.
  • NULL values are ignored — they are not treated as zero.
  • Use AVG(COALESCE(column, 0)) to count NULL as zero in the average.
  • Combine with WHERE to average a filtered subset, and with GROUP BY for per-group averages.
What's next? The next lesson covers GROUP BY — the clause that organises rows into groups so aggregate functions like COUNT, SUM, and AVG can work on each group separately.