mode helix
NEWIntroducing Helix—the first instant, responsive data engine.Learn more

SQL AVG

Starting here? This lesson is part of a full-length tutorial in using SQL for Data Analysis. Check out the beginning.

The SQL AVG function

AVG is a SQL aggregate function that calculates the average of a selected group of values. It's very useful, but has some limitations. First, it can only be used on numerical columns. Second, it ignores nulls completely. You can see this by comparing these two queries of the Apple stock prices dataset:

SELECT AVG(high)
  FROM tutorial.aapl_historical_stock_price
 WHERE high IS NOT NULL

The above query produces the same result as the following query:

SELECT AVG(high)
  FROM tutorial.aapl_historical_stock_price

There are some cases in which you'll want to treat null values as 0. For these cases, you'll want to write a statement that changes the nulls to 0 (covered in a later lesson.

Sharpen your SQL skills

Practice Problem

Write a query that calculates the average daily trade volume for Apple stock.

Try it out See the answer

Next Lesson

SQL GROUP BY

Looks like you've got a thing for cutting-edge data news.

Get the latest.

Get started