Database Tutorial

Course Tutorial Site

Site Admin

Right Join

without comments

The RIGHT [OUTER] JOIN matches every row in one table against every row in another based on common values in one or a set of columns, like an INNER JOIN. It then also returns everything from the table on the right of the RIGHT JOIN operator that was not returned as part of the match.

Another way to describe a RIGHT JOIN is as an intersection between two rows of data plus all rows found in the right set not found in the left set. The formula uses a ∀ to indicate given any sets A and B, then A intersects (∩) B. The Venn diagram and set notation depicts that definition.

The syntax of a RIGHT OUTER JOIN and RIGHT JOIN are equivalent because the OUTER keyword is optional in some databases and excluded in others. A RIGHT JOIN is a filtered join of the conceptual Cartesian Product of two tables and a relative complement. The filter identifies the rows that have matching values in a pair of columns, or a pair of two or more column sets. The relative complement of the RIGHT JOIN operator is the left relative complement.

A RIGHT JOIN acts like a nested loop where you match a key from the outer loop against a key from the inner loop, while preserving all non-matching values from the outer loop. An example written in PHP is found at the bottom of this page.

Written by michaelmclaughlin

January 17th, 2010 at 10:25 pm

Posted in

Leave a Reply