Find the average of column values in MySQL using aggregate function

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable
(
   Number int
);
Query OK, 0 rows affected (0.79 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(56);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values(78);
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable values(89);
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values(98);
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable values(91);
Query OK, 1 row affected (0.35 sec)
mysql> insert into DemoTable values(96);
Query OK, 1 row affected (0.15 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+--------+
| Number |
+--------+
|     56 |
|     78 |
|     89 |
|     98 |
|     91 |
|     96 |
+--------+
6 rows in set (0.00 sec)

Following is the query to get the average of column values −

mysql> select (sum(Number)*(100))/(100+500) from DemoTable;

This will produce the following output −

+-------------------------------+
| (sum(Number)*(100))/(100+500) |
+-------------------------------+
|                       84.6667 |
+-------------------------------+
1 row in set (0.00 sec)
raja
Published on 26-Sep-2019 06:48:01
Advertisements