Table of Contents
This guide covers handling copy - handling duplicate in SQL with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.
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. cotNFROM ten_bangWHERE [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 | +----+----------+-----+-----------+----------+
Start by we see how the SELECT Query returns a copy of LUONG:
SQL> SELECT SALARY FROM CUSTOMERSORDER 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 CUSTOMERSORDER 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
FAQ
What should I check before following these steps?
Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.
Why might the process not work?
Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.
Can I undo the changes if necessary?
That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.
Reader Comments 0
Sign in with email or Google to join the discussion.