SQL AVG Function

Definition and Usage

The AVG function returns the average of the numeric column. NULL values are not included in the calculation.

SQL AVG() Syntax

SELECT AVG(column_name) FROM table_name

SQL AVG() Example

We have the following 'Orders' table:

O_Id OrderDate OrderPrice Customer
1 2008/12/29 1000 Bush
2 2008/11/23 1600 Carter
3 2008/10/05 700 Bush
4 2008/09/28 300 Bush
5 2008/08/06 2000 Adams
6 2008/07/21 100 Carter

Example 1

Now, we want to calculate the average value of the 'OrderPrice' field.

We use the following SQL statement:

SELECT AVG(OrderPrice) AS OrderAverage FROM Orders

The result set is similar to this:

OrderAverage
950

Example 2

Now, we want to find customers whose OrderPrice is higher than the average OrderPrice.

We use the following SQL statement:

SELECT Customer FROM Orders
WHERE OrderPrice > (SELECT AVG(OrderPrice) FROM Orders)

The result set is similar to this:

Customer
Bush
Carter
Adams