ThoughtSpot acquires Mode to define the next generation of collaborative BI >>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.

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

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

Get more from your data

Your team can be up and running in 30 minutes or less.