
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count Duplicate Values in a MySQL 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)
Advertisements