SQL Joins
Join predicate - The condition specified after ON
in a JOIN
statement
Cross Join
Like the Cartesain product of rows from the tables in the join.
Explicit:
Implicit:
Inner Join
Joins the intersection between the two columns in the joined tables
Explicit (INNER
is optional):
Implicit:
Equi-join
A type of inner join where the join predicate looks like a.c = b.c
. In this case, ON a.c = b.c
can be replaced with USING (c)
. An inner join with an a.c = b.c
join predicate is called an equi-join whether or not the USING
notation is used.
Example:
Natural join:
- A special case of an equi-join. It is the “natural” way to join two tables that include common column names. The natural join of two tables R and S is the set of all combinations of rows in R and S that are equal on their common column (“attribute”) names.
- Natural join is the relational counterpart to a logical
AND
. - A natural join doesn’t require a join predicate, because it uses common column names as the implicit predicate.
- Natural joins are considered harmful, because inadvertantly naming two unrelated columns the same in two different tables can create unintented effects.
The syntax for a natural join is:
Outer Join
An outer join will retain rows that don’t satisfy the join-predicate, leaving some rows NULL
.
- A
LEFT OUTER JOIN
will retain rows in the table on the left side of theJOIN
keyword - A
RIGHT OUTER JOIN
will retain rows in the table to the right side of theJOIN
keyword. - A
FULL OUTER JOIN
will retain rows in the tables on both sides of theJOIN
keyword.
The outer keyword is optional. You can instead call these LEFT JOIN
, RIGHT JOIN
, FULL JOIN
. There is no implicit notation for outer joins.
Left Outer
Right Outer
Full Outer