TExcelTAD
Home › SQL › SQL AVG

SQL AVG

AVG(expression) returns the average (arithmetic mean) of the non-NULL values.

sql
SELECT AVG(column) FROM table_name WHERE condition;

Average price

sql · try it yourself
SELECT AVG(price) AS raw_average,
       ROUND(AVG(price), 2) AS rounded
FROM products;
raw_average        | rounded
-------------------+--------
22.529333333333334 | 22.53
(1 row)

Averages are rarely round numbers, so ROUND keeps the output readable.

Average per group

The average salary per job title:

sql · try it yourself
SELECT title, COUNT(*) AS people, ROUND(AVG(salary)) AS avg_salary
FROM employees
GROUP BY title
ORDER BY avg_salary DESC;
title              | people | avg_salary
-------------------+--------+-----------
CEO                | 1      | 185000
Sales Manager      | 1      | 98000
Operations Manager | 1      | 95000
Data Analyst       | 1      | 72000
Sales Rep          | 4      | 58875
Warehouse Lead     | 1      | 54000
Support Specialist | 1      | 51000
(7 rows)

Comparing rows with the average

Which products are more expensive than the average product? The average is computed in a subquery:

sql · try it yourself
SELECT name, price
FROM products
WHERE price > (SELECT AVG(price) FROM products)
ORDER BY price DESC;
name                        | price
----------------------------+------
Noise-Cancelling Headphones | 149
Mechanical Keyboard         | 89
Coffee Grinder              | 59.9
Chef Knife                  | 45
Learn Rust in Practice      | 39
Cast Iron Pan               | 38
Python Recipes              | 34.5
Power Bank                  | 29.5
The SQL Handbook            | 29
Wireless Mouse              | 24.99
Espresso Beans              | 24.5
(11 rows)

Average items per order

How many units does an average order contain? First sum the quantities per order in a subquery, then average those sums:

sql · try it yourself
SELECT ROUND(AVG(units), 2) AS avg_units_per_order
FROM (
  SELECT order_id, SUM(quantity) AS units
  FROM order_items
  GROUP BY order_id
);
avg_units_per_order
-------------------
6.97
(1 row)

Simply writing AVG(quantity) would give the average per order line, a different number.

AVG and NULL

NULLs are skipped, not treated as 0. That can change the meaning of an average:

sql · try it yourself
CREATE TABLE ratings (product TEXT, stars INTEGER);
INSERT INTO ratings VALUES ('Green Tea', 5), ('Green Tea', 3), ('Green Tea', NULL);

SELECT AVG(stars)              AS avg_ignoring_null,
       AVG(COALESCE(stars, 0)) AS avg_null_as_zero,
       SUM(stars) * 1.0 / COUNT(*) AS sum_over_rows
FROM ratings;
3 rows affected.

avg_ignoring_null | avg_null_as_zero   | sum_over_rows
------------------+--------------------+-------------------
4                 | 2.6666666666666665 | 2.6666666666666665
(1 row)

Integer averages in other databases

SQLite's AVG always returns a floating-point number. Some databases return an integer when the column is an integer type:

Database AVG of integers 1 and 2
SQLite, MySQL, PostgreSQL 1.5
SQL Server 1 (cast first: AVG(CAST(x AS FLOAT)))

Test yourself

What is AVG of the values 4, 6 and NULL?

  1. 3.33
  2. 5
  3. NULL
  4. 10

How do you list products priced above average?

  1. WHERE price > AVG(price)
  2. WHERE price > (SELECT AVG(price) FROM products)
  3. HAVING price > AVG
  4. ORDER BY AVG(price)