with t as ( select '0001' id, 'AAA' col1, 'BBB' col2, 'testtest' col3 from dual union all select '0002', 'BBB', 'AAA', 'testtest' from dual union all select '0003', 'CCC', 'CCC', 'testtest' from dual ) select * from ( select id, col1, col2, col3 , row_number() over(partition by least(col1,col2), greatest(col1,col2) order by id) rn from t ) where rn = 1