Open In App

SQL Query to Find Duplicate Names in a Table

Improve
Improve
Like Article
Like
Save
Share
Report

Duplicate records in a database can be problematic sometimes as they create confusion or they might generate wrong outputs as well. So, it is better to remove all the duplicate records from the database as it will save our time and space. However, the best practice is to put unique constraints (Primary key constraints) on a table to prevent that. 

      This error could be because of various reasons such as – 

  • Application flaws,
  • User error
  • Bad database design
  • Or because of some unknown external source.

So, in this article, we are going to find duplicate Names in a Table. To make that query we need to know about GROUP BY statement and Aggregate functions( specifically COUNT()).

We recommend you first go through this article for a better understanding.

To find the duplicate Names in the table, we have to follow these steps:

  • Defining the criteria:  At first, you need to define the criteria for finding the duplicate Names. You might want to search in a single column or more than that.
  • Write the query: Then simply write the query to find the duplicate Names.

Let’s get started-

Suppose you are working with a database of an e-commerce website. Now, some usernames are saved more than once and so are their email ids. This is going to cause erroneous analytical results for the e-commerce website as saving this data more than once is unnecessary. 

Now let’s first create our demo database,

Step 1: Creating the database

Create a new database named User_details and then use that.

Query:

CREATE DATABASE User_details; USE User_details;   

Output:

Step 2: Defining the table

Create a table named Users1 and add these four columns ID, Names, EmailId and Age.

Query:

CREATE Table Users1 (ID VARCHAR(20) Primary Key,
 Names VARCHAR(30), EmailId VARCHAR(30), Age INT);   

Output:

Step 3: Insert rows into the table and Insert these six rows in the table.

Query:

INSERT INTO Users1 VALUES('O1201', 'Radhika Malhotra', 'rad12@gmail.com', 21); 
INSERT INTO Users1 VALUES('O1202', 'Aryan Ray', 'Ar13@gmail.com', 25); 
INSERT INTO Users1 VALUES('O1203', 'Sam Das', 'Sam1@gmail.com', 54); 
INSERT INTO Users1 VALUES('O1204', 'Radhika Malhotra', 'rad12@gmail.com', 21);
 INSERT INTO Users1 VALUES('O1205', 'Aryan Ray', 'Ar13@gmail.com', 25);
  INSERT INTO Users1 VALUES('O1206', 'Radhika Malhotra', 'rad12@gmail.com', 21);   

Output:

Step 4: Viewing Inserted data

Run this command to see our table.

Query:

SELECT * FROM Users1;    

Output:

Users1 Table

Step 5: Now, let’s make our query to find duplicate Names in this table.

  • Defining the Criteria: Here we define criteria for only the Names column is selected from the Users1 table.

Query: 

SELECT Names,COUNT(*) AS Occurrence FROM
 Users1 GROUP BY Names HAVING COUNT(*)>1;   

This query is simple. Here, we are using the GROUP BY clause to group the identical rows in the Names column. Then we are finding the number of duplicates in that column using the COUNT() function and show that data in a new column named Occurrence. Having a clause only keeps the groups that have more than one occurrence.

Output:

We have found the duplicate Names and their Occurrence in our table. This information can help us to remove the duplicate rows from the table in the future.


Last Updated : 28 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads