Search code examples
sqlsql-serversql-updateouter-joinright-join

What is significance of right outer join in UPDATE statement


So a senior in my team is updating staging_table with the corresponding values in tb_party table. I could understand if he used an inner join (in which case, the staging_table would be updated for exact matches). But why is he using right outer join? Can somebody please explain the significance of right join in below query? What will happen to staging table if there is no match for certain records?

UPDATE C
    SET c.party_first_name = e.customername
        ,c.party_type = 
        CASE 
        WHEN e.ClassificationType is not null THEN e.ClassificationType
        ELSE c.party_type
        END
        ,c.tax_indicator = 
        case
        when c.party_type='entity' then e.TaxNonTaxGovernmentIDNumberEntitiesTaxNonTaxGovernmentIDType
        Else NULL
        END
    FROM staging_table C
        right join MTB_AML.dbo.tb_party E
        on c.party_key = E.CustomerInternalID

Solution

  • He joins the E table to set the values in `C' to values based on the different updates. Let's break it down.

    c.party_first_name = e.customername
    

    Here, if there is no match, the party_first_name will be set to NULL

    c.party_type = 
        CASE 
        WHEN e.ClassificationType is not null THEN e.ClassificationType
        ELSE c.party_type
        END
    

    Here, if there is no match, the party_type will stay whatever it is set in C, with no change. If there is a match, then it will set party_type to e.classificationType

    c.tax_indicator = 
        case
        when c.party_type='entity' then e.TaxNonTaxGovernmentIDNumberEntitiesTaxNonTaxGovernmentIDType
        Else NULL
        END
    

    Here, if the party_type is equal to entity then it will take whatever value is in e.TaxNonTaxGovernmentIDNumberEntitiesTaxNonTaxGovernmentIDType regardless of a match or not. If the party_type does not equal entity then it will be set to NULL