MySQL AVG() Function
The AVG()
function in MySQL calculates the average (mean) of a set of numeric values. It's a fundamental aggregate function used to summarize numerical data.
AVG(): Definition and Usage
AVG()
is incredibly useful for getting a quick overview of the central tendency of your data. It automatically ignores any NULL
values that might be present in the column you're averaging. This means you don't need to pre-filter out NULL
s before using AVG()
.
Syntax
Syntax
AVG(expression)
Parameter Values
Parameter | Description |
---|---|
expression |
A numeric column or expression that evaluates to a number. This is required. |
Examples
Calculating the Average Price
This example calculates the average price of products from a 'Products' table (assuming it has a 'Price' column).
Syntax
SELECT AVG(Price) AS AveragePrice FROM Products;
Output
AveragePrice
------------
18.29 (Example output; this will vary based on your data)
Finding Products Above Average Price
This query uses a subquery to find products with a price higher than the average price.
Syntax
SELECT * FROM Products
WHERE Price > (SELECT AVG(Price) FROM Products);
Output
ProductID | ProductName | Price
------------------------------
1 | Product A | 22.00 (Example; this output will depend on your data)
2 | Product B | 25.50 (Example; this output will depend on your data)