Search code examples
sqloracle-databasedistinct

How to select distinct multi-column values in Oracle SQL?


I am trying to get distinct values with multi column select.

Sample table:

CREATE TABLE DUP_VALUES (ID NUMBER, NAME VARCHAR2(64));

 INSERT INTO DUP_VALUES values (1, 'TEST1');
 INSERT INTO DUP_VALUES values (2, 'TEST1');
 INSERT INTO DUP_VALUES values (3, 'TEST2');
 INSERT INTO DUP_VALUES values (4, 'TEST2');
 INSERT INTO DUP_VALUES values (5, 'TEST1');
 INSERT INTO DUP_VALUES values (6, 'TEST1');
 INSERT INTO DUP_VALUES values (7, 'TEST1');

I want to get

ID NAME
1  TEST1
3  TEST2

I tried with SELECT DISTINCT ID, NAME FROM DUP_VALUES

But, I got all values, because ID is unique.


Solution

  • Use aggregation:

    select min(id) as id, name
    from dup_values
    group by name;