web analytics

Understanding SQL Joins

Options
@2016-01-14 13:29:09

Full Join

A full join will give you the union of A and B, i.e. All the rows in A and all the rows in B. If something in A doesn't have a corresponding datum in B, then the B portion is null, and vice versa.

select A.Id, B.Id
from TableA A 
  FULL JOIN TableB B
  on A.Id = B.Id;

The result will be

 A.Id  |  B.Id 

-------+-------

     1 | null

     2 | null

     3 |    3

     4 |    4

  null |    6

  null |    5

If you just want rows eith in A or in B, then you have to add a where caluse a.a is null or b.b is null to exclude any common rows.

select A.Id, B.Id
from TableA A 
  FULL JOIN TableB B 
  on A.Id = B.Id
where 
 (A.Id is null) or (B.Id is null);

The result will be

 A.Id  |  B.Id

-------+-------

     1 | null

     2 | null

  null |    6

  null |    5

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com