CodeBlanks Logo Icon CodeBlanks

AVG

Compute the average.

AVG

AVG returns the average of a column. It ignores NULL values.

SELECT AVG(price) FROM products;

This query gives the average price.

AVG(price) is selected from products.

AVG — details

AVG divides the sum by the row count. NULLs are excluded from both sum and count.

For example, the average of 10, 20 and NULL is 15 (30 / 2); to count NULL as 0 you write AVG(COALESCE(price, 0)).

Practice AVG

Compute the average price in products with AVG(price).

Starter code

query.sql
-- average price
SELECT

Sample solution

query.sql
SELECT AVG(price) FROM products;

Expected output

avg
233.33

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 AVG: match the terms

Term Meaning
AVG average
SUM sum
NULL skip
numeric numeric

2 Add AVG

Question
SELECT ____(price) FROM products;
Answer
SELECT AVG(price) FROM products;

Explanation: AVG(price) returns the average price; NULL values are ignored.

3 How does AVG handle NULLs?

  • Skips Correct answer
  • Raises an error
  • Counts them as zero
  • Includes them in the count

Explanation: AVG skips NULL values.

All lessons in Intermediate