Home > Net >  SQL Joins to retrieve NULL values for rows which are not present in another table
SQL Joins to retrieve NULL values for rows which are not present in another table

Time:04-13

I have 2 tables in DB2 as below:

Table A:

Key     ID  SubID
Abc123  576  10
Abc124  577  12
Abc125  578  14

Table B:

ID  SubID
576  10
576  11
577  12
577  13
578  14
578  15

I want output something like this :

  Key     ID  SubID
  Abc123  576  10
  Null    576  11
  Abc124  577  12
  Null    577  13
  Abc125  578  14
  Null    578  15

I joined table A and table B on ID column by using Left/Inner joins but still I am not getting the desired output as mentioned above .

Appreciate any help!

CodePudding user response:

It seems you just need an outer join, have you tried

Select a.Key, b.Id, b.SubId
from TableB b
left join TableA a on a.ID = b.ID and a.SubID = b.SubID;
  • Related