Home » SQL

SQL Query to avoid duplicate records

In this article, we are going to learn about duplicate records and the process to avoid them in our ResultSet while selecting the data.
Submitted by Manu Jemini, on March 14, 2018

It’s one of the best practices to always use distinct when we don’t want to have duplicate records. The Approach of not having duplicate records is so popular that we have a keyword called distinct which will do the rest of job.

Anyhow, we can do it manually but will take a lot of effort, usually when we don’t have an idea about the field which can have duplicate values. Impact of having these keywords is such that we can make complex query simple.

If you are struggling to understand what I mean by the duplicate records, then imagine a scenario where you have a single table of product and the table has a field which stores the category name. Now there can be different products with the same category name.

Now, this isn’t a case when we have our data in a repetitive manner. So, to get the category from this type of table all we need to do is to use the distinct keyword.

Table (employee) having three fields and four data in it,

    Id		Name		Address

    100		Aman		Mumbai
    200		Arun		Pune
    300		Karan		Delhi
    400		Aman		Mumbai
    500		Sumit		Indore

In above table the data of row one and four is same for column name and address and we need to avoid the duplicate row of data in ResultSet so all we need to do is to use DISTINCT keyword before the name of column we are going to fetch and it will give us the unique rows of data.

Query 1:

SELECT distinct name FROM employee;

Output

SQL Query to avoid duplicate records 1

Query 2:

SELECT distinct address FROM employee;

Output

SQL Query to avoid duplicate records 2



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.