Find duplicate column values in MySQL and display them

MySQLMySQLi Database

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)
raja
Published on 26-Dec-2019 06:39:26
Advertisements