MySQL query to count number of duplicate values in a table column


Let us first create a table −

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

Insert some records in the table using insert command −

mysql> insert into DemoTable values(60);
Query OK, 1 row affected (0.27 sec)
mysql> insert into DemoTable values(40);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values(50);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values(60);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values(40);
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable values(80);
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 −

+------+
| Data |
+------+
| 60   |
| 40   |
| 50   |
| 60   |
| 40   |
| 80   |
+------+
6 rows in set (0.00 sec)

Following is the query to count the number of duplicate values in a column −

mysql> select count(*) from DemoTable group by Data having count(Data) > 1;

This will produce the following output −

+----------+
| count(*) |
+----------+
| 2        |
| 2        |
+----------+
2 rows in set (0.03 sec)

Updated on: 27-Sep-2019

144 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements