SQL: Removing Duplicate records - Albeit different kind SQL: Removing Duplicate records - Albeit different kind oracle oracle

SQL: Removing Duplicate records - Albeit different kind


Use GREATEST() and LEAST() functions to identify the common values across multiple columns. Then use DISTINCT to winnow out the duplicates.

select distinct least(a, b) as a       , greatest(a, b) as b       , cfrom t6 

This gives you the precise record set you asked for. But things will get more complicated if you need to include other columns from T6.


"But I was wondering if this will work for VARCHAR2 fields also?"

Yes but it will use ASCII values to determine order, which is not always what you might expect (or desire).

"Also, my table T6 might have tens of thousand of records."

That really isn't a lot of data in today's terms. The DISTINCT will cause a sort, which should be able to fit in memory unless A and B are really long VARCHAR2 columns - but probably even then.

If this is a query you're going to want to run a lot then you can build a function-based index to satisfy it:

create index t6_fbi on t6(least(a, b)                           , greatest(a, b)                           , c )/

But I would really only bother if you have a genuine performance issue with the query.


If the order of columns A and B do not matter and always contain an integer, how about:

select distinct  least(a, b) as a,  greatest(a, b) as b,  cfrom  t6