![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculating percentage in a MySQL query and round off the result
For this, you can use CONCAT() and round(). Let us first create a table −
mysql> create table DemoTable1844 ( Number int, TotalNumber int ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1844 values(50,500); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1844 values(80,500); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1844 values(98,500); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1844 values(45,500); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1844;
This will produce the following output −
+--------+-------------+ | Number | TotalNumber | +--------+-------------+ | 50 | 500 | | 80 | 500 | | 98 | 500 | | 45 | 500 | +--------+-------------+ 4 rows in set (0.00 sec)
Here is the query to calculating percentage in a query and rounding off the result −
mysql> select concat(round(((Number / TotalNumber) * 100 ),2), '%') as Result from DemoTable1844;
This will produce the following output −
+--------+ | Result | +--------+ | 10.00% | | 16.00% | | 19.60% | | 9.00% | +--------+ 4 rows in set (0.00 sec)
Advertisements