0
How to delete duplicate Records in SQL
1 Answer
+ 1
DELETE DUPLICATE ROWS FROM A TABLE IN SQL SERVER 2005
CREATE TABLE EMPLOYEE
(
id INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
EMPNAME VARCHAR(100),
SALARY DECIMAL
)
SELECT * FROM EMPLOYEE
INSERT INTO EMPLOYEE VALUES ( âPawanâ , 100000 )
INSERT INTO EMPLOYEE VALUES ( âPawanâ , 80000 )
INSERT INTO EMPLOYEE VALUES ( âPawanâ , 40000 )
INSERT INTO EMPLOYEE VALUES ( âRahulâ , 100000 )
INSERT INTO EMPLOYEE VALUES ( âPawanâ , 100000 )
INSERT INTO EMPLOYEE VALUES ( âGauriâ , 100000 )
INSERT INTO EMPLOYEE VALUES ( âGauriâ , 100000 )
INSERT INTO EMPLOYEE VALUES ( âRahulâ , 100000 )
INSERT INTO EMPLOYEE VALUES ( âDeepaâ , 100000 )
âQuery to delete duplicate names in the above table
DELETE from EMPLOYEE where id NOT IN (SELECT MAX(id) FROM EMPLOYEE GROUP BY EmpName)
âCheck with
SELECT * FROM EMPLOYEE
Note : If you do not have identity column then create one and remove that column after your deletion.