Find percentage from marks in MySQL

MySQLMySQLi Database

Let us first create a −

mysql> create table DemoTable1398
   -> (
   -> Marks int
   -> );
Query OK, 0 rows affected (0.50 sec)

Insert some records in the table using insert −

mysql> insert into DemoTable1398 values(78);
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable1398 values(82);
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable1398 values(90);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1398 values(98);
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select −

mysql> select * from DemoTable1398;

This will produce the following output −

+-------+
| Marks |
+-------+
|    78 |
|    82 |
|    90 |
|    98 |
+-------+
4 rows in set (0.00 sec)

Following is the query to find percentage of marks −

mysql> select concat(sum(Marks)/count(*),'%') from DemoTable1398;

This will produce the following output −

+---------------------------------+
| concat(sum(Marks)/count(*),'%') |
+---------------------------------+
| 87.0000%                        |
+---------------------------------+
1 row in set (0.00 sec)
raja
Updated on 11-Nov-2019 10:42:56

Advertisements