Pre Course Home Next

MS SQL Server Interview Questions Tutorial

SQL Server Queries related Interview questions

Q. How to delete duplicate records?
A. Here We will list multiple ways you can delete the duplicate rows.

Delete duplicate rows using CTE

By using common table expression(CTE) we can easily remove duplicates. So for example, say i want to delete duplicate rows from customer table, and we determine duplicate based on company name, then the query will be:

with cte as
(
select CustomerId, ROW_NUMBER()over(PARTITION by CompanyName order by CustomerId) as rn from Customers
)

--select * from cte where rn > 1
Delete from cte where rn > 1

 

Pre Course Home Next