- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculate average of numbers in a column MySQL query?
152 Lectures 16 hours
87 Lectures 5.5 hours
Calculate the average of numbers in a column with the help of MySQL aggregate function AVG().
The syntax is as follows −
select avg(yourColumnName) as anyVariableName from yourTableName;
To understand the above concept, let us create a table. The following is the query to create a table.
mysql> create table AverageCalculateDemo −> ( −> SubjectMarks int −> ); Query OK, 0 rows affected (0.67 sec)
The following is the query to insert some records into the table −
mysql> insert into AverageCalculateDemo values(70); Query OK, 1 row affected (0.14 sec) mysql> insert into AverageCalculateDemo values(80); Query OK, 1 row affected (0.19 sec) mysql> insert into AverageCalculateDemo values(65); Query OK, 1 row affected (0.13 sec) mysql> insert into AverageCalculateDemo values(55); Query OK, 1 row affected (0.13 sec) mysql> insert into AverageCalculateDemo values(60); Query OK, 1 row affected (0.23 sec)
Display all values with the help of a select statement. The query is as follows to display all records −
mysql> select *from AverageCalculateDemo;
The following is the output −
+--------------+ | SubjectMarks | +--------------+ | 70 | | 80 | | 65 | | 55 | | 60 | +--------------+ 5 rows in set (0.00 sec)
Here is the query that calculates the average of the column in MySQL −
mysql> select avg(SubjectMarks) as AverageOf4Numbers from AverageCalculateDemo;
The following is the output that displays the average −
+-------------------+ | AverageOf4Numbers | +-------------------+ | 66.0000 | +-------------------+ 1 row in set (0.00 sec)
- MySQL query to calculate the average of values in a row?
- MongoDB query to calculate average
- MySQL query to select average from distinct column of table?
- C++ Program to Calculate Average of Numbers Using Arrays
- Java program to calculate the average of numbers in Java
- Golang Program to Calculate the Average of Numbers in a Given List
- Calculate average of column values and display the result with no decimals in MySQL
- MySQL query to remove a value with only numbers in a column
- Calculate average value in MongoDB
- Average of a stream of numbers in C++
- MySQL query to find the average of only first three values from a column with five values
- How to calculate average of a particular field in MongoDB?
- Create a MySQL function and find the average of values in a column
- Get the Average of Average in a single MySQL row?
- Average of Squares of Natural Numbers?