Find the percentage of a student on basis of marks and add percent sign (%) to the result in SQL

MySQLMySQLi Database

Let us first create a table −

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

Insert some records in the table using insert command −

mysql> insert into DemoTable values(88);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values(65);
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values(98);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values(45);
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values(67);
Query OK, 1 row affected (0.33 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-------+
| Marks |
+-------+
| 88    |
| 65    |
| 98    |
| 45    |
| 67    |
+-------+
5 rows in set (0.00 sec)

Following is the query to calculate percentage of the student in the basis of marks in 5 subjects. The result would be in percentage; therefore, we will also add percent sign (%) to the calculation −

mysql> select concat(round(SUM(Marks)*100/500),'%') from DemoTable;

This will produce the following output −

+---------------------------------------+
| concat(round(SUM(Marks)*100/500),'%') |
+---------------------------------------+
| 73%                                   |
+---------------------------------------+
1 row in set (0.00 sec)
raja
Published on 07-Oct-2019 11:22:23
Advertisements