Last Updated: 29 June, 2023
The AVG function in MySQL is used to calculate the average value of a set of numerical values. It is commonly used with the SELECT statement to retrieve the average value of a column in a table.
The syntax for the AVG function is as follows:
SELECT AVG(column_name) FROM table_name;
Here's a breakdown of the syntax:
Here's an example that demonstrates the usage of the AVG function.
Let's assume we have a table called "grades" with a column "score" containing numeric values:
CREATE TABLE grades ( id INT PRIMARY KEY, score INT ); INSERT INTO grades (id, score) VALUES (1, 90), (2, 85), (3, 95), (4, 80), (5, 92);
To calculate the average score from the "score" column, we can use the AVG function as follows:
SELECT AVG(score) FROM grades;
The result of this query would be:
+-----------+ | AVG(score)| +-----------+ | 88.4 | +-----------+
So, the average score in this example is 88.4.
That's all, guys. I hope this MySQL article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com