title: Joining tables if the reference exists
body: <p>I got a PostgreSQL database with 4 tables:</p>
  
  <p><strong>Table A</strong></p>
  
  <pre><code>---------------------------
  | ID | B_ID | C_ID | D_ID |
  ---------------------------
  | 1  |  1   | NULL | NULL |
  ---------------------------
  | 2  | NULL |   1  | NULL |
  ---------------------------
  | 3  |  2   |   2  |   1  |
  ---------------------------
  | 4  | NULL | NULL |   2  |
  ---------------------------
  </code></pre>
  
  <p><strong>Table B</strong></p>
  
  <pre><code>-------------
  | ID | DATA |
  -------------
  | 1  | 123  |
  -------------
  | 2  | 456  |
  -------------
  </code></pre>
  
  <p><strong>Table C</strong></p>
  
  <pre><code>-------------
  | ID | DATA |
  -------------
  | 1  | 789  |
  -------------
  | 2  | 102  |
  -------------
  </code></pre>
  
  <p><strong>Table D</strong></p>
  
  <pre><code>-------------
  | ID | DATA |
  -------------
  | 1  | 654  |
  -------------
  | 2  | 321  |
  -------------
  </code></pre>
  
  <p>I'm trying to retrieve a result set which has joined the data from table B and the data from table C, only if one of booth IDs is not null.</p>
  
  <pre><code>SELECT "Table_A"."ID", "Table_A"."ID_B", "Table_A"."ID_C", "Table_A"."ID_D", "Table_B"."DATA", "Table_C"."DATA"
      FROM "Table_A"
          LEFT JOIN "Table_B" on "Table_A"."ID_B" = "Table_B"."ID"
          LEFT JOIN "Table_C" on "Table_A"."ID_C" = "Table_C"."ID"
      WHERE "Table_A"."ID_B" IS NOT NULL OR "Table_A"."ID_C" IS NOT NULL;
  </code></pre>
  
  <p>Is this recommended or should I better split this in multiple queries?</p>
  
  <p>Is there a way to do an inner join between these tables?</p>
  
  <p>The result I expect is:</p>
  
  <pre><code>-------------------------------------------------
  | ID | ID_B | ID_C | ID_D | DATA (B) | DATA (C) |
  -------------------------------------------------
  | 1  |   1  | NULL | NULL |   123    |   NULL   |
  -------------------------------------------------
  | 2  | NULL |  1   | NULL |   NULL   |   789    |
  -------------------------------------------------
  | 3  |   2  |  2   | NULL |   456    |   102    |
  -------------------------------------------------
  </code></pre>
  
  <p><strong>EDIT:</strong>  <code>ID_B</code>, <code>ID_C</code>, <code>ID_D</code> are foreign keys to the tables <code>table_b</code>, <code>table_c</code>, <code>table_d</code></p>