MySQL numeric functions
MySQL string functions
MySQL Date/Time functions
MySQL aggregate functions
MySQL flow control functions
The AVG (Average) function is an aggregate function in MySQL that calculates the average value of a set of numbers. It is commonly used in SQL queries to analyze numerical data and perform statistical analysis.
In this tutorial, we will go over the syntax and usage of the AVG function in MySQL.
Syntax:
The syntax for the AVG function is as follows:
AVG(column_name)
Where column_name
is the name of the column containing the numeric values you want to average.
The AVG function can be used in SELECT statements, as well as in the GROUP BY and HAVING clauses. It ignores NULL values and considers only non-NULL numeric values in the column.
Examples:
Assume we have a table called sales
with the following columns: id
, product_id
, price
, and date
. To calculate the average price of all sales, you can use the following SQL query:
SELECT AVG(price) AS average_price FROM sales;
This query will return a single value that represents the average price of all non-NULL price
values in the sales
table.
Suppose you want to find the average price of sales for each product. You can use the AVG function in combination with the GROUP BY clause:
SELECT product_id, AVG(price) AS average_price FROM sales GROUP BY product_id;
This query will return a result set with the product_id
and the corresponding average price
for each product.
If you want to filter the results based on a condition related to the average price, you can use the HAVING clause. For example, you might want to find all products with an average sales price greater than 50:
SELECT product_id, AVG(price) AS average_price FROM sales GROUP BY product_id HAVING average_price > 50;
This query will return a result set with the product_id
and the average price
for each product with an average price greater than 50.
Conclusion:
In this tutorial, we've covered the syntax and usage of the AVG function in MySQL. You can use this function to calculate the average value of a set of numbers in a column, and combine it with the GROUP BY and HAVING clauses for more advanced statistical analysis. Keep in mind that the AVG function ignores NULL values and only considers non-NULL numeric values when performing calculations.
How to use AVG function in MySQL:
SELECT AVG(column_name) AS average_value FROM table_name;
MySQL AVG function examples:
SELECT AVG(price) AS avg_price FROM products;
Calculating average in MySQL with AVG:
SELECT AVG(column_name) FROM table_name;
AVG function in MySQL for numeric values:
SELECT AVG(column1 + column2) AS avg_sum FROM table_name;
Finding average values in MySQL:
SELECT AVG(score) AS avg_score FROM students;
AVG function in MySQL for grouped data:
SELECT category, AVG(price) AS avg_price FROM products GROUP BY category;
Use cases for AVG function in MySQL queries:
SELECT * FROM sales WHERE AVG(amount) > 1000;
AVG function in MySQL for positive and negative values:
SELECT AVG(temperature) AS avg_temp FROM weather_data;
Mathematical operations with AVG in MySQL:
SELECT AVG(column1 * 2) AS avg_doubled FROM table_name;
MySQL AVG function in WHERE clause:
SELECT * FROM data WHERE AVG(column_name) > 50;
AVG function in MySQL for date and time values:
SELECT AVG(TIMESTAMPDIFF(MINUTE, start_time, end_time)) AS avg_duration FROM events;