Let us first create a table −
mysql> create table DemoTable ( Id int, ColorName varchar(100) ); Query OK, 0 rows affected (0.56 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(100,'Red'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(101,'Green'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(101,'Blue'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values(102,'Yellow'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(100,'Purple'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(100,'Black'); Query OK, 1 row affected (0.26 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------+-----------+ | Id | ColorName | +------+-----------+ | 100 | Red | | 101 | Green | | 101 | Blue | | 102 | Yellow | | 100 | Purple | | 100 | Black | +------+-----------+ 6 rows in set (0.00 sec)
Following is the query to display the record with non-duplicate Id −
mysql> select *from DemoTable where Id in(select Id from DemoTable group by Id having count(*) < 2);
This will produce the following output −
+------+-----------+ | Id | ColorName | +------+-----------+ | 102 | Yellow | +------+-----------+ 1 row in set (0.07 sec)