
- 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
MySQL query to calculate the average of values in a row?
To calculate the average of values in a row in MySQL, use the following syntax
SELECT (yourTableName.yourColumnName1+yourTableName.yourColumnName2+yourTableName.yourColumnName3+,..........N)/numberOfColumns AS anyAliasName FROM yourTableName;
To understand the above syntax, let us create a table. The query to create a table is as follows
mysql> create table calculateAverageDemo -> ( -> x int, -> y int, -> z int -> ); Query OK, 0 rows affected (1.41 sec)
Insert some records in the table using insert command.
The query is as follows
mysql> insert into calculateAverageDemo values(10,20,30); Query OK, 1 row affected (0.78 sec) mysql> insert into calculateAverageDemo values(40,50,70); Query OK, 1 row affected (0.26 sec) mysql> insert into calculateAverageDemo values(80,90,220); Query OK, 1 row affected (0.43 sec)
Display all records from the table using select statement.
The query is as follows
mysql> select *from calculateAverageDemo;
The following is the output
+------+------+------+ | x | y | z | +------+------+------+ | 10 | 20 | 30 | | 40 | 50 | 70 | | 80 | 90 | 220 | +------+------+------+ 3 rows in set (0.00 sec)
Here is the query to calculate average of values in a row
mysql> select (calculateAverageDemo.x+calculateAverageDemo.y+calculateAverageDemo.z)/3 -> AS Average from calculateAverageDemo;
The following is the output
+----------+ | Average | +----------+ | 20.0000 | | 53.3333 | | 130.0000 | +----------+ 3 rows in set (0.06 sec)
- Related Articles
- Calculate average of numbers in a column MySQL query?
- Get the Average of Average in a single MySQL row?
- MongoDB query to calculate average
- MySQL query to find the average of only first three values from a column with five values
- Get the average row length of a MySQL table
- Calculate average of column values and display the result with no decimals in MySQL
- MySQL query to update every alternative row string having same values?
- MySQL query to delete row
- Querying average row length in MySQL?
- MySQL query to concatenate all the values in each row based on the common matching ID
- Sum values of a single row in MySQL?
- MySQL Query a List of Values?
- MySQL query to return all items in a single row
- MySQL query to calculate the total amount from column values with Cost and Quantity?
- MySQL query to group concat and place data into a single row on the basis of 1 values in corresponding column?

Advertisements