When dealing with large datasets, it’s common to encounter duplicate records in a table. Duplicate data can cause inaccurate record fetching results if it is not managed appropriately. To ensure accuracy, we must retrieve unique records instead of duplicates.
To solve this issue, SQL provides the DISTINCT keyword, which is used with the SELECT statement to eliminate duplicate records and fetch only unique ones.
Complete Python Course with Advance topics:-Click Here SQL Tutorial :-Click Here
Using DISTINCT to Remove Duplicates
The DISTINCT keyword helps retrieve unique values from a specific column or a combination of columns. Below is the syntax:
SELECT DISTINCT column1, column2, ....columnN
FROM table_name
WHERE [conditions];
Example
Let’s consider a EMPLOYEE table with duplicate salary records.
EMP_ID
NAME
SALARY
CITY
101
RAHUL VERMA
50000
MUMBAI
102
NEHA SINGH
48000
DELHI
103
ARJUN SHARMA
50000
BANGALORE
104
SAKSHI JAIN
52000
KOLKATA
105
ROHAN MEHTA
48000
PUNE
106
KRITI AGGARWAL
51000
CHENNAI
Checking for Duplicate Salary Records
Before applying DISTINCT, let’s retrieve all salary records to check for duplicates:
SELECT SALARY FROM EMPLOYEE
ORDER BY SALARY;
Output:
SALARY
48000
48000
50000
50000
51000
52000
Using DISTINCT to Remove Duplicates
Now, we apply the DISTINCT keyword to fetch unique salary records:
SELECT DISTINCT SALARY FROM EMPLOYEE
ORDER BY SALARY;
Output:
SALARY
48000
50000
51000
52000
Duplicate salary values have been removed, as can be seen above.
Download New Real Time Projects :-Click here Complete Advance AI topics:-ย CLICK HERE
Conclusion
In order to preserve data integrity, SQL must handle duplicate records. The DISTINCT keyword is a simple yet powerful solution to fetch unique records from a table, ensuring accurate and meaningful data retrieval.
For more SQL tutorials and updates, stay tuned to UpdateGadh!
sql delete duplicate rows keep one delete duplicate rows in sql w3schools to remove duplicate rows in sql select statement sql delete duplicate rows with same id sql delete duplicate rows but keep one without primary key sql delete duplicate rows based on one column delete duplicate rows in oracle delete duplicate records in sql server using row_number sql delete duplicate rows but keep one cte sql sql delete duplicate rows oracle
Leave a Reply