The SQLite AVG
function calculates and returns the average of all values in a set of non-NULL
values. Typically, a set of values represents the values of multiple records in a column. The SQLite AVG function is an aggregate type function and is used in calculations.
Syntax
Here is the syntax of SQLite AVG function:
SELECT AVG(column_name) FROM table_name;
Syntax of SQLite AVG function with GROUP BY
:
SELECT column_name1, AVG(column_name2) FROM table_name GROUP BY column_name1;
Examples
Basic AVG example
SELECT AVG(b.price) FROM books b;
The first example shows how to calculate the average price of a book from the books table.
AVG with GROUP BY
SELECT c.customer_id, AVG(c.amount) FROM orders c GROUP BY c.customer_id;
The second example shows how to calculate the average purchases per customer. The SELECT statement uses GROUP BY to group the returned rows by customer_id.