0% found this document useful (0 votes)
61 views2 pages

Deleting Duplicate Rows in SQL Tables

The document describes 3 methods for deleting duplicate rows from a table: 1. Using the ROWID to delete rows except those with the maximum ROWID for each duplicate group. 2. Creating a temporary table with DISTINCT values and truncating the original table before inserting the temporary table values. 3. Using ROW_NUMBER to rank rows within duplicate groups and selecting only the first ranked row of each group into a temporary table before truncating and inserting from the temporary table.

Uploaded by

jmsreddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views2 pages

Deleting Duplicate Rows in SQL Tables

The document describes 3 methods for deleting duplicate rows from a table: 1. Using the ROWID to delete rows except those with the maximum ROWID for each duplicate group. 2. Creating a temporary table with DISTINCT values and truncating the original table before inserting the temporary table values. 3. Using ROW_NUMBER to rank rows within duplicate groups and selecting only the first ranked row of each group into a temporary table before truncating and inserting from the temporary table.

Uploaded by

jmsreddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Different Ways (How) to Delete Duplicate

Rows in Table
In this article I am going to show different ways of deleting duplicating records from the table. It
is a common question in interviews which is asked frequently.
Consider the following table with rows as an example:
Table Name: Products
ProductId Price
--------------1
10
1
10
2
20
3
30
3
30

Here assume that productId column should be unique after deleting. Now we see how to delete
the duplicate records from the products table in different ways.
1. Using Rowid
The following Delete statement deletes the rows using the Rowid.
Syntax:

Delete from <tablename>


where rowid not in (select max(rowid) from <tablename> group by <unique
columns or primary key>);
Example:
Delete from products
where rowid not in (select max(rowid) from products group by productid);

2. Using temp table and Distinct


Here, first create a temp table and insert distinct rows in the temp table. Then truncate the main
table and insert records from the temp table.
Create temporary table products_temp As
Select Distinct ProductID, Price
From
Products;
Truncate table Products;
Insert into products
Select *
From
products_temp;

3. Using temp table and Row Number analytic function.


The row_number analytic function is used to rank the rows. Here we use the row_number
function to rank the rows for each group of productId and then select only record from the group.
Create temporary table products_temp As
Select productid, price
From
(
Select productid, price,
row_number() over (partition by productId order by price) group_rank
From
products
)
Where group_rank = 1;

Please comment here if you know any other methods of deleting the duplicate records from the
table.

You might also like