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;


SQL Query to avoid duplicate records 1

Query 2:

SELECT distinct address FROM employee;


SQL Query to avoid duplicate records 2

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.