
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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)
- Related Articles
- MySQL query to calculate the average of values in a row?
- MySQL query to select average from distinct column of table?
- MongoDB query to calculate average
- MySQL query to remove a value with only numbers in a column
- Calculate average of column values and display the result with no decimals in MySQL
- MySQL query to find the average of only first three values from a column with five values
- Golang Program to Calculate the Average of Numbers in a Given List
- MySQL query to calculate sum from 5 tables with a similar column named “UP”?
- Java program to calculate the average of numbers in Java
- Can I search for particular numbers in a MySQL column with comma separated records using a MySQL query?
- C++ Program to Calculate Average of Numbers Using Arrays
- How to calculate the average in a column based on criteria in another column in Excel?
- Create a MySQL function and find the average of values in a column
- MySQL query to replace a column value
- MySQL query to remove Null Records in a column?

Advertisements