MySQL query to skip the duplicate and select only one from the duplicated values


The syntax is as follows to skip the duplicate value and select only one from the duplicated values −

select min(yourColumnName1),yourColumnName2 from yourTableName group by
yourColumnName2;

To understand the above syntax, let us create a table. The query to create a table is as follows −

mysql> create table doNotSelectDuplicateValuesDemo
   -> (
   -> User_Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> User_Name varchar(20)
   -> );
Query OK, 0 rows affected (0.78 sec)

Now you can insert some records in the table using insert command. The query is as follows −

mysql> insert into doNotSelectDuplicateValuesDemo(User_Name) values('John');
Query OK, 1 row affected (0.15 sec)
mysql> insert into doNotSelectDuplicateValuesDemo(User_Name) values('Carol');
Query OK, 1 row affected (0.09 sec)
mysql> insert into doNotSelectDuplicateValuesDemo(User_Name) values('Carol');
Query OK, 1 row affected (0.17 sec)
mysql> insert into doNotSelectDuplicateValuesDemo(User_Name) values('Carol');
Query OK, 1 row affected (0.08 sec)
mysql> insert into doNotSelectDuplicateValuesDemo(User_Name) values('Sam');
Query OK, 1 row affected (0.28 sec)
mysql> insert into doNotSelectDuplicateValuesDemo(User_Name) values('Mike');
Query OK, 1 row affected (0.19 sec)
mysql> insert into doNotSelectDuplicateValuesDemo(User_Name) values('Bob');
Query OK, 1 row affected (0.16 sec)
mysql> insert into doNotSelectDuplicateValuesDemo(User_Name) values('David');
Query OK, 1 row affected (0.21 sec)
mysql> insert into doNotSelectDuplicateValuesDemo(User_Name) values('Maxwell');
Query OK, 1 row affected (0.13 sec)
mysql> insert into doNotSelectDuplicateValuesDemo(User_Name) values('Bob');
Query OK, 1 row affected (0.11 sec)
mysql> insert into doNotSelectDuplicateValuesDemo(User_Name) values('Ramit');
Query OK, 1 row affected (0.16 sec)

Display all records from the table using select statement. The query is as follows −

mysql> select *from doNotSelectDuplicateValuesDemo;

Here is the output −

+---------+-----------+
| User_Id | User_Name |
+---------+-----------+
| 1       | John      |
| 2       | Carol     |
| 3       | Carol     |
| 4       | Carol     |
| 5       | Sam       |
| 6       | Mike      |
| 7       | Bob       |
| 8       | David     |
| 9       | Maxwell   |
| 10      | Bob       |
| 11      | Ramit     |
+---------+-----------+
11 rows in set (0.00 sec)

Here is the query to skip the duplicate value and select only one from the duplicated values −

mysql> select min(User_Id),User_Name from doNotSelectDuplicateValuesDemo group by
User_Name;

Here is the output −

+--------------+-----------+
| min(User_Id) | User_Name |
+--------------+-----------+
| 1            | John      |
| 2            | Carol     |
| 5            | Sam       |
| 6            | Mike      |
| 7            | Bob       |
| 8            | David     |
| 9            | Maxwell   |
| 11           | Ramit     |
+--------------+-----------+
8 rows in set (0.07 sec)

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements