Sunday, September 6, 2009

70-433 SQL Server INNER, OUTER, LEFT & RIGHT JOINS (Venn diagrams) and CROSS JOIN (Cartesian products)

Supposes you have two set, let's just focus on the key values on which you are going to perform the joins: Keys1 = {A,B,C} and Keys2 = {B,C,D}


"Venn diagram/sets"

Keys1 LEFT JOIN Keys2 gives Keys {A,B,C}

Keys1 RIGHT JOIN Keys2 gives Keys {B,C,D}

Keys1 INNER JOIN Keys2 gives {C,D}  (default/vanilla join) 

Keys1 OUTER JOIN Keys2 {A,B,C,D}

Keys1 JOIN Keys2 gives {C,D}


"Every pair combination (aka Cartesian product)"

Keys1 CROSS JOIN Keys2  {AB, AC, AD, BB, BC, BD, CB, CC, CD} 

i.e 3x3 = 9 pair combinations


Cartesian product

noun Mathematics

the product of two sets: the product of set X and set Y is the set that contains all ordered pairs ( x, y) for which x belongs to X and y belongs to Y.


No comments: