Saturday, July 24, 2010

Write a SQL query which deletes the duplicate rows (entire rows are duplicates). Keep the original row and delete the all the same rows. (Assuming there are 4 duplicate rows, keep the first row and delete other three rows)

 Employee table                                                                                                          
 empName
deptName
Raju Das
Engg
Shashi
Engg
Prashant
Engg
Raju Das
Engg
Prashant
Engg
Raju Das
Engg
Shashi
Engg
Ravi
Mamagement

with cte(empName, deptName,rowid) as
(
      select *, ROW_NUMBER() over (PARTITION by empname,deptname order by empname,deptname) as rowid
      from employee1
)

delete from cte where rowid > 1
Using cte function, update and select operation can only be done

No comments:

Post a Comment


All Rights Reserved @ Raju Das