For this, you need to use GROUP BY for the column name. To get the count, use COUNT(*) and order the result with ORDER BY. Following is the syntax −
select count(*) as anyAliasName from yourTableName group by yourColumnName order By yourAliasName DESC;
Let us first create a table −
mysql> create table DemoTable ( Number int ); Query OK, 0 rows affected (0.82 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(90); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(110); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(90); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(90); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(120); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(90); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(120); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------+ | Number | +--------+ | 90 | | 100 | | 110 | | 90 | | 100 | | 90 | | 120 | | 100 | | 90 | | 120 | +--------+ 10 rows in set (0.00 sec)
Following is the query to display the count of duplicate records and order the result −
mysql> select count(*) as Occured from DemoTable group by Number order By Occured DESC;
This will produce the following output −
+---------+ | Occured | +---------+ | 4 | | 3 | | 2 | | 1 | +---------+ 4 rows in set (0.00 sec)