Inner Join, Self Join and Outer Join: Sen Zhang
Inner Join, Self Join and Outer Join: Sen Zhang
Sen Zhang
Joining data together is one of the most significant strengths of a relational database. A join is a query that combines rows from two or more relations. Joins allow database users to combine data from one table with data from one or more other tables or views, or synonyms, as long as they are relations.
Tables are joined two at a time making a new relation (a table generated on the fly) containing all possible combinations of rows from the original two tables (sometimes called a cross join or Cartesian product). See sample script
A join condition is usually used to limit the combinations of table data to just those rows containing columns that match columns in the other table. Most joins are equi-joins where the data from a column in one table exactly matches data in the column of another table.
It is also possible (though usually less efficient) to join using ranges of values or other comparisons between the tables involved. A table may be joined to another table, tables, or even itself (reused multiple times).
It is important to understand that whenever two or more tables/views/synonyms (in fact, they are all relations) are listed in a FROM clause, a join results. Join conditions serve the purpose of limiting the number of rows returned by the join. The absence of a join condition results in all possible combinations of rows from the involved tables, i.e. a Cartesian product, which is usually not useful information.
Inner Joins
An inner join (sometimes called a simple join) is a join of two or more tables that returns only those rows that satisfy the join condition.
Inner Join
Traditional inner joins look for rows that match rows in the other table(s), i.e. to join two tables based on values in one table being equal to values in another table Also known as equality join, equijoin or natural join Returns results only if records exist in both tables
Self-Join
A query that joins a table to itself, for example, employee table can be joined to itself to find out subordinate - supervisor pairs. Used when a table has a foreign key relationship to itself (usually parent-child relationship) Must create a table alias and structure the query as if you are joining the table to a copy of itself
FROM table1 alias1, ... Use alias, not table name for select and where clauses
Self-Join Example
Outer Join
Returns all rows in one table and matching rows in joined table Inner table: all rows are returned, in other word, if all rows need to be returned for one table, that table is called inner table. Outer table: matching rows are returned Outer table marked with a + in join condition inner_table.join_field = outer_table.join_field(+) Null values are inserted for fields in outer table that are not found
Full join
How about if we want to include rows in both tables that cannot find match in the opposite tables? It is called full join. Until now, the only way to accomplish a full join (values missing on both sides of a query) was to Union the results of both left outerjoin and right outerjoin.