A SQL JOIN
clause combines records from two tables in a relational database, resulting in a new, temporary table, sometimes called a "joined table". A JOIN
may also be thought of as a SQL operation that relates tables by means of values common between them. SQL specifies four types of JOIN
: INNER
, OUTER
, LEFT
, and RIGHT
. In special cases, a table (base table, view, or joined table) can JOIN
to itself in a self-join.
A programmer writes a JOIN
predicate to identify the records for joining. If the predicate evaluates positively, the combined record is inserted into the temporary (or "joined") table. Any predicate supported by SQL can become a JOIN
-predicate, for example, WHERE
-clauses.
An inner join requires each record in the two joined tables to have a matching record. An inner join essentially combines the records from two tables (A and B) based on a given join-predicate. The result of the join can be defined as the outcome of first taking the Cartesian product (or cross-join) of all records in the tables (combining every record in table A with every record in table B) - then return all records which satisfy the join predicate. Actual SQL implementations will normally use other approaches where possible, since computing the Cartesian product is not very efficient. This type of join occurs most commonly in applications, and represents the default join-type.
SQL:2003 specifies two different syntactical ways to express joins. The first, called "explicit join notation", uses the keyword JOIN
, whereas the second uses the "implicit join notation". The implicit join notation lists the tables for joining in the FROM
clause of a SELECT
statement, using commas to separate them. Thus, it specifies a cross-join, and the WHERE
clause may apply additional filter-predicates. Those filter-predicates function comparably to join-predicates in the explicit notation.
One can further classify inner joins as equi-joins, as natural joins, or as cross-joins (see below).
Programmers should take special care when joining tables on columns that can contain NULL values, since NULL will never match any other value (or even NULL itself), unless the join condition explicitly uses the IS NULL
or IS NOT NULL
predicates.
Equi-join
An equi-join, also known as an equijoin, is a specific type of comparator-based join, or theta join, that uses only equality comparisons in the join-predicate. Using other comparison operators (such as <
) disqualifies a join as an equi-join. The query shown above has already provided an example of an equi-join:
SELECT Employee.lastName, Employee.DepartmentID, Department.DepartmentName
FROM Employee INNER JOIN Department
ON Employee.DepartmentID = Department.DepartmentID
ORDER BY Employee.lastName;
The resulting joined table contains two columns named DepartmentID, one from table Employee and one from table Department
SQL:2003 does not have a specific syntax to express equi-joins, but some database engines provide a shorthand syntax: for example, MySQL and PostgreSQL support USING(DepartmentID)
in addition to the ON ...
syntax.
[edit] Natural join
A natural join offers a further specialization of equi-joins. The join predicate arises implicitly by comparing all columns in both tables that have the same column-name in the joined tables. The resulting joined table contains only one column for each pair of equally-named columns.
Cross join
A cross join, cartesian join or product provides the foundation upon which all types of inner joins operate. A cross join returns the cartesian product of the sets of records from the two joined tables. Thus, it equates to an inner join where the join-condition always evaluates to True or join-condition is absent in statement.
If A and B are two sets, then the cross join is written as A × B.
The SQL code for a cross join lists the tables for joining (FROM
), but does not include any filtering join-predicate.
Example of an explicit cross join:
SELECT *
FROM employee CROSS JOIN department
Example of an implicit cross join:
SELECT *
FROM employee, department;
Outer joins
An outer join does not require each record in the two joined tables to have a matching record. The joined table retains each record—even if no other matching record exists. Outer joins subdivide further into left outer joins, right outer joins, and full outer joins, depending on which table(s) one retains the rows from (left, right, or both).
(For a table to qualify as left or right its name has to appear after the FROM
or JOIN
keyword, respectively.)
No implicit join-notation for outer joins exists in SQL:2003.
[edit] Left outer join
The result of a left outer join (or simply left join) for tables A and B always contains all records of the "left" table (A), even if the join-condition does not find any matching record in the "right" table (B). This means that if the ON
clause matches 0 (zero) records in B, the join will still return a row in the result—but with NULL in each column from B. This means that a left outer join returns all the values from the left table, plus matched values from the right table (or NULL in case of no matching join predicate).
For example, this allows us to find an employee's department, but still to show the employee even when their department does not exist (contrary to the inner-join example above, where employees in non-existent departments are excluded from the result).
Example of a left outer join, with the additional result row italicized:
SELECT *
FROM employee LEFT OUTER JOIN department
Right outer join
A right outer join (or right join) closely resembles a left outer join, except with the tables reversed. Every record from the "right" table (B) will appear in the joined table at least once. If no matching row from the "left" table (A) exists, NULL will appear in columns from A for those records that have no match in A.
A right outer join returns all the values from the right table and matched values from the left table (NULL in case of no matching join predicate).
For example, this allows us to find each employee and their department, but still show departments that have no employees.
Example right outer join, with the additional result row italicized:
SELECT *
FROM employee RIGHT OUTER JOIN department
Full outer join
A full outer join combines the results of both left and right outer joins. The joined table will contain all records from both tables, and fill in NULLs for missing matches on either side.
For example, this allows us to see each employee who is in a department and each department that has an employee, but also see each employee who is not part of a department and each department who doesn't have an employee.
Example full outer join:
SELECT *
FROM employee
FULL OUTER JOIN department
ON employee.DepartmentID = department.DepartmentID
ON employee.DepartmentID = department.DepartmentID
ON employee.DepartmentID = department.DepartmentID
No comments:
Post a Comment