Skip to Main Content
  • Questions
  • how to delete duplicate records from table

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, vikesh.

Asked: August 20, 2007 - 7:19 am UTC

Last updated: August 22, 2007 - 1:28 pm UTC

Version: 9.1.1

Viewed 1000+ times

You Asked

hi tom,

how to delete duplicate records from table
without the use of rowid

rgds

and Tom said...

you don't, without rowid there is no way to distinguish them.

You would be hard pressed to do this without someway to identify a record, to make it unique.


for database independent solutions - well, I don't do those anymore, I gave up on getting correctly executing, performant, cross database applications in 1992...

You can get correct
You can get fast
You cannot get correct and fast however... You usually in fact end up with slow and wrong...



I can only tell you how to do things right in Oracle.
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:4301254394074


Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Deleting Duplicates

Dan, August 21, 2007 - 4:59 pm UTC

Well, if by "duplicate records" you mean that all of the column values in each row are the same, you could "select them out" using the row_number function.  For example:

SQL> create table xxx (a1 varchar2(1), b2 varchar2(1), c3 varchar2(1));

Table created.

SQL> insert into xxx values ('a','b','c');

1 row created.

SQL> insert into xxx values ('a','b','c');

1 row created.

SQL> insert into xxx values ('a','b','c');

1 row created.

SQL> commit;

Commit complete.

SQL> select * from xxx;

A B C
- - -
a b c
a b c
a b c

SQL> create table zzz as
  2  (select a1, b2, c3
  3   from
  4     (select a1, b2, c3, row_number() over
  5             (partition by a1,b2,c3 order by a1,b2,c3) rn  
  6      from xxx)
  7   where rn=1);

Table created.

SQL> select * from zzz;

A B C
- - -
a b c

Then, just drop xxx and rename zzz to xxx.

Tom Kyte
August 22, 2007 - 12:33 pm UTC

that is what I describe as one of the options in the in the link - not using analytics - just select distinct * from t; would do it.

Duplicate Records

Dan, August 21, 2007 - 5:11 pm UTC

Sorry, the test data in the last example was a little weak.  This is a bit better:

SQL> create table xxx (a1 varchar2(1), b2 varchar2(1), c3 varchar2(1));

Table created.

SQL> insert into xxx values ('a','b','c');

1 row created.

SQL> insert into xxx values ('a','b','c');

1 row created.

SQL> insert into xxx values ('a','b','c');

1 row created.

SQL> insert into xxx values ('d','e','f');

1 row created.

SQL> insert into xxx values ('d','e','f');

1 row created.

SQL> commit;

Commit complete.

SQL> select * from xxx;

A B C
- - -
a b c
a b c
a b c
d e f
d e f

SQL> create table zzz as
  2  (select a1, b2, c3
  3   from
  4     (select a1, b2, c3, row_number() over
  5             (partition by a1,b2,c3 order by a1,b2,c3) rn  
  6      from xxx)
  7   where rn=1);

Table created.

SQL> select * from zzz;

A B C
- - -
a b c
d e f

Tom Kyte
August 22, 2007 - 12:35 pm UTC

just use distinct, analytics complicate things here - not appropriate for this.

To Dan

A reader, August 21, 2007 - 10:08 pm UTC

Dan, Can you not just do,

create table zzz as
select distinct * from xxx;

??

Thanks.

Tom Kyte
August 22, 2007 - 1:28 pm UTC

yes.