Inner Join, Self Join and Outer Join: Sen Zhang
Inner Join, Self Join and Outer Join: Sen Zhang
Inner Join, Self Join and Outer Join: Sen Zhang
join
Sen Zhang
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.