Monday, April 8, 2019

SQL Server : Delete duplicate rows from table


Delete duplicate rows from table , we are considering duplicate rows based on Name and City column Values.

--Works well if Table Desn't have primary key

WITH TEMP AS (
SELECT ROW_NUMBER() OVER ( PARTITION BY NAME,CITY ORDER BY NAME,CITY) as RN,NAME,CITY 
FROM TAbleDuplicate
)  
DELETE FROM TEMP WHERE
RN>1

Another way to delete duplicate from table (if primary key is present in table):

DELETE FROM EMPLOYEE WHERE EMP_ID NOT IN (
SELECT Min(EMP_ID) FROM EMPLOYEE
GROUP BY FIRST_NAME,LAST_NAME,SALARY ) 

No comments:

Post a Comment

C# Record type: Something to remember while using record types

  Record in c# provide a concise and expressive way to create immutable data types, record is a keyword in c#, we can use this keyword with ...