totn SQL

SQL: AVG Function

This SQL tutorial explains how to use the SQL AVG function with syntax and examples.

Description

The SQL AVG function is used to return the average of an expression in a SELECT statement.

Syntax

The syntax for the AVG function in SQL is:

SELECT AVG(aggregate_expression)
FROM tables
[WHERE conditions];

OR the syntax for the AVG function when grouping the results by one or more columns is:

SELECT expression1, expression2, ... expression_n,
       AVG(aggregate_expression)
FROM tables
[WHERE conditions]
GROUP BY expression1, expression2, ... expression_n;

Parameters or Arguments

expression1, expression2, ... expression_n
Expressions that are not encapsulated within the AVG function and must be included in the GROUP BY clause at the end of the SQL statement.
aggregate_expression
This is the column or expression that will be averaged.
tables
The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.
WHERE conditions
Optional. These are conditions that must be met for the records to be selected.

Example - With Single Expression

For example, you might wish to know how the average cost of all products that are in the Clothing category.

SELECT AVG(cost) AS "Average Cost"
FROM products
WHERE category = 'Clothing';

In this SQL AVG Function example, we've aliased the AVG(cost) expression as "Average Cost". As a result, "Average Cost" will display as the field name when the result set is returned.

Example - Using SQL DISTINCT

You can use the SQL DISTINCT clause within the AVG function. For example, the SELECT statement below returns the combined average cost of unique cost values where the category is Clothing.

SELECT AVG(DISTINCT cost) AS "Average Cost"
FROM products
WHERE category = 'Clothing';

If there were two cost values of $25, only one of these values would be used in the AVG function calculation.

Example - Using Formula

The expression contained within the AVG function does not need to be a single field. You could also use a formula. For example, you might want the average profit for a product. Average profit is calculated as sale_price less cost.

SELECT AVG(sale_price - cost) AS "Average Profit"
FROM products;

You might also want to perform a mathematical operation within the AVG function. For example, you might determine the average commission as 10% of sale_price.

SELECT AVG(sale_price * 0.10) AS "Average Commission"
FROM products;

Example - Using SQL GROUP BY

In some cases, you will be required to use the SQL GROUP BY clause with the AVG function.

For example, you could also use the AVG function to return the name of the department and the average sales (in the associated department).

SELECT department, AVG(sales) AS "Average Sales"
FROM order_details
WHERE department > 10
GROUP BY department;

Because you have listed one column in your SELECT statement that is not encapsulated in the AVG function, you must use the GROUP BY clause. The department field must, therefore, be listed in the GROUP BY section.