Find duplicate column values in MySQL and display them


For this, use GROUP BY HAVING clause. Let us first create a table −

mysql> create table DemoTable1858
     (
     ModelNumber varchar(50)
     );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1858 values('Audi A4');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1858 values('Audi A6');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1858 values('Audi A4');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1858 values('Audi Q5');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1858 values('Audi R8');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1858 values('Audi Q5');
Query OK, 1 row affected (0.00 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1858;

This will produce the following output −

+-------------+
| ModelNumber |
+-------------+
| Audi A4     |
| Audi A6     |
| Audi A4     |
| Audi Q5     |
| Audi R8     |
| Audi Q5     |
+-------------+
6 rows in set (0.00 sec)

Here is the query to find duplicate column value in MySQL −

mysql> select ModelNumber from DemoTable1858
     group by ModelNumber
     having count(ModelNumber) >=2;

This will produce the following output −

+-------------+
| ModelNumber |
+-------------+
| Audi A4     |
| Audi Q5     |
+-------------+
2 rows in set (0.00 sec)

Updated on: 26-Dec-2019

443 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements