oracle intrw

2
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

Upload: jmsreddy

Post on 15-Sep-2015

213 views

Category:

Documents


1 download

DESCRIPTION

Oracle

TRANSCRIPT

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 101 102 203 303 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 where rowid not in (select max(rowid) from group by );

Example:

Delete from productswhere 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 AsSelect Distinct ProductID, PriceFrom Products;

Truncate table Products;

Insert into productsSelect * 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 AsSelect productid, priceFrom( 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.