Search code examples
sqloracleoracle11g

Oracle SQLScript to match columns


Currently I'm working on a task to join the columns of 2 tables & match them and update in the third column whether they are matching or not.

select a.ID,a.NAME,b.NAME from TABLEA a join TABLEB B on a.ID=b.ID;

This is how table looks after join

So, my first row is matching & 2nd one is not. I want to create a third col which matches these col and produce result in third col (IS_MATCHED) in the form of true or false.

How to do this??


Solution

  • One option is to use case expression.

    Sample data:

    SQL> with
      2  tablea (id, name) as
      3    (select 1, 'utkarsh' from dual union all
      4     select 2, 'vedansh' from dual
      5    ),
      6  tableb (id, name) as
      7    (select 1, 'utkarsh' from dual union all
      8     select 2, 'rahul'   from dual
      9    )
    

    Query begins here:

     10  select a.id, a.name, b.name,
     11    --
     12    case when a.name = b.name then 'match'
     13         else null
     14    end is_matched
     15  from tablea a join tableb b on a.id = b.id;
    
            ID NAME    NAME    IS_MATCHED
    ---------- ------- ------- ----------
             1 utkarsh utkarsh match
             2 vedansh rahul
    
    SQL>