Get the count of duplicate values from a single column in MySQL?

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable
   -> (
   -> Number int
   -> );
Query OK, 0 rows affected (0.83 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(20);
Query OK, 1 row affected (0.23 sec)

mysql> insert into DemoTable values(20);
Query OK, 1 row affected (0.14 sec)

mysql> insert into DemoTable values(10);
Query OK, 1 row affected (0.23 sec)

mysql> insert into DemoTable values(30);
Query OK, 1 row affected (0.14 sec)

mysql> insert into DemoTable values(10);
Query OK, 1 row affected (0.47 sec)

mysql> insert into DemoTable values(10);
Query OK, 1 row affected (0.15 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+--------+
| Number |
+--------+
|     20 |
|     20 |
|     10 |
|     30 |
|     10 |
|     10 |
+--------+
6 rows in set (0.00 sec)

Following is the query to select count values from a single column −

mysql> select Number,count(*) from DemoTable group by Number;

This will produce the following output −

+--------+----------+
| Number | count(*) |
+--------+----------+
|     20 |        2 |
|     10 |        3 |
|     30 |        1 |
+--------+----------+
3 rows in set (0.00 sec)
raja
Published on 10-Jul-2019 08:41:39
Advertisements