Syntax error Calculate average of column values and display the result with no decimals in MySQL

Calculate average of column values and display the result with no decimals in MySQL



For this, you can use round() along with avg(). Let us first create a table −

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

Insert some records in the table using insert command −

mysql> insert into DemoTable values(98);
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable values(97);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values(91);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values(86);
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable values(45);
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-------+
| Score |
+-------+
|    98 |
|    97 |
|    91 |
|    86 |
|    45 |
+-------+
5 rows in set (0.00 sec)

Following is the query to calculate the average of column values and display the result with no decimals −

mysql> select round(avg(Score),0) AS Average from DemoTable;

This will produce the following output −

+---------+
| Average |
+---------+
|      83 |
+---------+
1 row in set (0.00 sec)
Updated on: 2019-10-01T07:42:23+05:30

321 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements