Click here to Skip to main content
15,888,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table call EMP, which have 3 columns (EMP_NAME, EMP_MAIL, EMP_CONTACT) and does not have any primary key. Now i want to delete the duplicate row from this table using SQL inline query.

Below is the example.

emp_name emp_mail emp_contact

Arijita abcd@xyz 8122020706
Arijit abc@xy 8122020705
Arijit abc@xy 8122020705
Arijit abc@xy 8122020705

I search a query which will help me to delete the duplicate values.

Output should be :

emp_name || emp_mail || emp_contact

Arijita || abcd@xyz || 8122020706
Arijit || abc@xy || 8122020705

What I have tried:

I have found that how many duplicates are there.
Posted
Updated 11-Aug-16 2:00am

You can write a query for that something like following-
SQL
;WITH CTE(emp_name,emp_mail,emp_contact,SlNo)
AS
(
    SELECT emp_name,emp_mail,emp_contact,ROW_NUMBER() OVER(PARTITION BY emp_name,emp_mail,emp_contact ORDER BY emp_name,emp_mail,emp_contact) AS SlNo
    FROM Demo
)
DELETE FROM CTE WHERE SlNo > 1


For more details please visit my blog post at-
SQL Server: Delete/Update duplicate records | Technology Talks[^]

Hope, it helps :)
 
Share this answer
 
v3
Comments
Richard Deeming 28-Jul-16 16:29pm    
If you specify the column aliases in the "header" of the CTE, you have to give an alias for all columns. You're currently missing SlNo.

Either:
WITH CTE(emp_name,emp_mail,emp_contact,SlNo) As ...

or:
WITH CTE As ...

would work.
Suvendu Shekhar Giri 28-Jul-16 16:35pm    
Thanks Richard for pointing out the mistake.. updated the answer.
Karthik_Mahalingam 29-Jul-16 0:54am    
5
 
Share this answer
 
;WITH CTE
AS
(
SELECT emp_name,emp_mail,emp_contact,ROW_NUMBER() OVER(PARTITION BY emp_name,emp_mail,emp_contact ORDER BY emp_name,emp_mail,emp_contact) AS No
FROM EMP
)
DELETE FROM CTE WHERE No > 1
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900