Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Calculate average of numbers in a column MySQL query?
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)
Advertisements
