SQL AVG
The AVG() function is an aggregate function used to calculate the average (mean) value of a numeric column.
👉 In simple terms:
AVG() adds all the numeric values and divides by the number of values.
Important Points
1️. AVG() works only with numeric columns
2️. It ignores NULL values
3️. Often used with GROUP BY for data analysis
4️. Can be used with expressions like Quantity * Price
Syntax
SELECT AVG(column_name) FROM table_name;
Example: Find the average price.
SELECT AVG(Price) AS AveragePrice FROM Orders;
AVG with WHERE: Find the average price where Quantity > 2
SELECT AVG(Price) AS AveragePrice FROM Orders WHERE Quantity > 2;
AVG with GROUP BY: Find the average quantity per customer
SELECT CustomerID, AVG(Quantity) AS AvgQuantity FROM Orders GROUP BY CustomerID;
AVG with Expression: Find average order value
SELECT AVG(Quantity * Price) AS AvgOrderValue FROM Orders;
Calculate the average order amount for each customer.
SELECT CustomerID,
AVG(Quantity * Price) AS AvgOrderAmount
FROM Orders
GROUP BY CustomerID;
Simple Definition (Exam/Interview):
AVG() is an SQL aggregate function used to calculate the average value of a numeric column, ignoring NULL values.