Count top 10 most occurring values in a column in MySQL?

MySQLMySQLi Database

To count the top 10 most occurring values in a column in MySQL, The syntax is as follows −

SELECT yourColumnName, count(*)
   FROM yourTableName
   GROUP BY yourColumnName
   ORDER BY count(*) DESC
   LIMIT 10;

To understand the above syntax, let us create a table. The query to create a table is as follows −

mysql> create table countTop10Demo
   -> (
   -> Value int
   -> );
Query OK, 0 rows affected (0.74 sec)

Insert some records in the table using insert command. The query is as follows −

mysql> insert into countTop10Demo values(10);
Query OK, 1 row affected (0.12 sec)
mysql> insert into countTop10Demo values(1100);
Query OK, 1 row affected (0.11 sec)
mysql> insert into countTop10Demo values(200);
Query OK, 1 row affected (0.11 sec)
mysql> insert into countTop10Demo values(2100);
Query OK, 1 row affected (0.10 sec)
mysql> insert into countTop10Demo values(1100);
Query OK, 1 row affected (0.11 sec)
mysql> insert into countTop10Demo values(450);
Query OK, 1 row affected (0.28 sec)
mysql> insert into countTop10Demo values(600);
Query OK, 1 row affected (0.12 sec)
mysql> insert into countTop10Demo values(2100);
Query OK, 1 row affected (0.16 sec)
mysql> insert into countTop10Demo values(150);
Query OK, 1 row affected (0.16 sec)
mysql> insert into countTop10Demo values(16454);
Query OK, 1 row affected (0.10 sec)
mysql> insert into countTop10Demo values(450);
Query OK, 1 row affected (0.12 sec)
mysql> insert into countTop10Demo values(2350);
Query OK, 1 row affected (0.10 sec)
mysql> insert into countTop10Demo values(1780);
Query OK, 1 row affected (0.15 sec)
mysql> insert into countTop10Demo values(1345);
Query OK, 1 row affected (0.22 sec)
mysql> insert into countTop10Demo values(34);
Query OK, 1 row affected (0.15 sec)
mysql> insert into countTop10Demo values(2100);
Query OK, 1 row affected (0.08 sec)
mysql> insert into countTop10Demo values(19034);
Query OK, 1 row affected (0.12 sec)
mysql> insert into countTop10Demo values(1844);
Query OK, 1 row affected (0.11 sec)
mysql> insert into countTop10Demo values(34);
Query OK, 1 row affected (0.08 sec)
mysql> insert into countTop10Demo values(16454);
Query OK, 1 row affected (0.19 sec)

Now you can Display all records from the table using select statement. The query is as follows −

mysql> select *from countTop10Demo;

Here is the output −

+-------+
| Value |
+-------+
| 10    |
| 1100  |
| 200   |
| 2100  |
| 1100  |
| 450   |
| 600   |
| 2100  |
| 150   |
| 16454 |
| 450   |
| 2350  |
| 1780  |
| 1345  |
| 34    |
| 2100  |
| 19034 |
| 1844  |
| 34    |
| 16454 |
+-------+
20 rows in set (0.00 sec)

The following is the query to select top 10 most occurring values in a column in MySQL −

mysql> SELECT Value, count(*)
   -> FROM countTop10Demo
   -> GROUP BY Value
   -> ORDER BY count(*) DESC
   -> LIMIT 10;

Here is the output −

+-------+----------+
| Value | count(*) |
+-------+----------+
| 2100  | 3        |
| 1100  | 2        |
| 34    | 2        |
| 450   | 2        |
| 16454 | 2        |
| 1780  | 1        |
| 200   | 1        |
| 1345  | 1        |
| 19034 | 1        |
| 600   | 1        |
+-------+----------+
10 rows in set (0.00 sec)
raja
Published on 01-Apr-2019 09:51:19
Advertisements