Handling copy - HANDLING DUPLICATE in SQL

There is a situation where data has multiple duplicate records in a table and you want to retrieve a single record rather than retrieving all duplicate records. To handle this situation, use the DISTINCT keyword in SQL in conjunction with the SELECT statement, you will remove all duplicate records and retrieve only records.

Syntax

The basic syntax of the DISTINCT keyword to remove duplicates is as follows:

 SELECT DISTINCT cot1, cot2. cotN 
FROM ten_bang
WHERE [dieu_kien]

Examples of how to handle Duplicate in SQL

Considering the table NHANVIEN has the following records:

 +----+----------+-----+-----------+----------+ | ID | TEN |TUOI | DIACHI | LUONG | +----+----------+-----+-----------+----------+ | 1 | Thanh | 32 | Haiphong | 2000.00 | | 2 | Loan | 25 | Hanoi | 1500.00 | | 3 | Nga | 23 | Hanam | 2000.00 | | 4 | Manh | 25 | Hue | 6500.00 | | 5 | Huy | 27 | Hatinh | 8500.00 | | 6 | Cao | 22 | HCM | 4500.00 | | 7 | Lam | 24 | Hanoi | 10000.00 | +----+----------+-----+-----------+----------+

First, we see how the SELECT query returns a copy of LUONG:

 SQL> SELECT SALARY FROM CUSTOMERS 
ORDER BY SALARY;

In the resulting result, LUONG 2000 appears twice, and is a copy from the original table.

 +----------+ | LUONG | +----------+ | 1500.00 | | 2000.00 | | 2000.00 | | 4500.00 | | 6500.00 | | 8500.00 | | 10000.00 | +----------+ 

Now, use the DISTINCT keyword with the SELECT query and see the result:

 SQL> SELECT DISTINCT SALARY FROM CUSTOMERS 
ORDER BY SALARY;

In the results, you will not see any copies.

+----------+ | LUONG | +----------+ | 1500.00 | | 2000.00 | | 4500.00 | | 6500.00 | | 8500.00 | | 10000.00 | +----------+ 

Previous post: CLONE TABLE in SQL

Next lesson: Query SUBQUERY child in SQL

4 ★ | 1 Vote

May be interested

  • How to create duplicate search queries in Access 2016How to create duplicate search queries in Access 2016
    duplicate search queries allow you to search and identify duplicate records in a table or multiple tables. duplicate records are a record that refers to the same or the same person as another record.
  • How to Find and Remove Duplicate Songs in iTunesHow to Find and Remove Duplicate Songs in iTunes
    are you constantly filling up your ipod with multiple copies of the same track? when you hit next, does the same song play again? if so, then you've got a duplicate file problem. luckily, it's a pretty easy one to fix. follow this guide to...
  • Duplicate the slide in PowerPointDuplicate the slide in PowerPoint
    introduce how to duplicate slide in powerpoint. to duplicate slides with the same format and content to save time and effort, for example, there is one slide: you want to duplicate slides into multiple copies with the same format and content as follows: mouse
  • Exception handling - Exception Handling in PythonException handling - Exception Handling in Python
    in this article, quantum will work with you to learn how to handle exceptions in python using try, except and finally. this will motivate you to write more neat, readable and efficient code.
  • Software to scan duplicate files on your computer costs 19.95 USD, is freeSoftware to scan duplicate files on your computer costs 19.95 USD, is free
    finding and deleting duplicate files with similar content on your computer will significantly save your hard drive's storage space. the professional software below will help you do this.
  • How to remove duplicates in excelHow to remove duplicates in excel
    to delete the same lines and figures in excel data sheet, we can use available features like remove duplicate or manually delete through the hightlight section.
  • Application aggregation deletes the duplicate phone number on iPhoneApplication aggregation deletes the duplicate phone number on iPhone
    this is a collection of applications that help you delete duplicate contacts, or delete contact names without numbers in your contacts on your iphone.
  • Guide to find and delete duplicate photos in your computer using the Duplicate Images FinderGuide to find and delete duplicate photos in your computer using the Duplicate Images Finder
    sometimes in the process of data storage it is very likely that you save the same data file in different locations. the following article details how to find duplicate photos on your computer.
  • How to Find Duplicate Data in ExcelHow to Find Duplicate Data in Excel
    when working on microsoft excel spreadsheets with lots of data, it is likely that you will encounter duplicate values. microsoft excel's conditional formatting feature will correctly display duplicate locations, while the remove duplicates action will remove those entries. reviewing and removing duplicates ensures the accuracy of your data and presentation.
  • The best 5 apps to delete photos on AndroidThe best 5 apps to delete photos on Android
    today, smartphones are a popular means of taking photos of amateurs. after a while, your device will become messy with the same or not-so-good photos. the article will introduce you to the 5 best duplicate photo removal apps on android.