SQL Tutorial

SQL Introduction SQL Aggregate Functions SQL Aliases SQL And SQL Any All SQL Avg SQL Between SQL Case SQL Comments SQL Count SQL Delete SQL Distinct SQL Exists SQL GROUP BY SQL Having SQL In SQL INSERT INTO SQL Is Not Null SQL Join SQL Full Outer Join SQL Inner Join SQL Left Join SQL Right Join SQL Self Join SQL Like SQL Min Max SQL NOT Operator SQL Null SQL Operators SQL OR operator SQL ORDER BY SQL Select SQL Select Into SQL Top Limit Fetch SQL Stored Procedures SQL Sum SQL Union SQL Update SQL Where SQL Wildcards

SQL Database

SQL Alter Table SQL Auto increment SQL Backup Database SQL Check SQL Constraints SQL Create View SQL Create Database SQL Create Table SQL Data types SQL Dates SQL Default Constraint SQL Drop Database SQL Drop Table SQL Foreign Key SQL Hosting SQL Index SQL injections SQL Not NULL SQL PrimaryKey SQL Unique SQL Views

SQL AVG

The AVG() function returns the average value of a numeric column. It is useful for prices, scores, ages, quantities and any other number where the center of the data matters.

Syntax

SELECT AVG(column_name)
FROM table_name;

AVG() ignores NULL values. That is usually what you want, but it can surprise you if missing values mean zero in your business rule.

Example

Get the average price of all products:

SELECT AVG(price) AS average_price
FROM products;

AVG with GROUP BY

Average values become more useful when you group rows by a category:

SELECT category_id, AVG(price) AS average_price
FROM products
GROUP BY category_id;

This returns one average for each category. Without GROUP BY, the query returns one average for the whole table.

Common mistakes

Related topics

Continue with SQL COUNT, SQL SUM, SQL GROUP BY and the SQL Formatter.