SQL AVG

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

In this lesson we'll cover:

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

white-wave
SQL Practice Icon

Practice Problem


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

Try it out

SQL Query Editor


Available tables:
Try running the sample queries or write your own!