An SQL
JOIN
clause combines records from two or more tables in a database. It creates a set that can be saved as a table or used as is. A
JOIN
is a means for combining fields from two tables by using values common to each. ANSI standard SQL specifies four types of
JOIN
s:
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 evaluated predicate is true, the combined record is then produced in the expected format, a record set or a temporary table, for example.
Novices should beware that the default
JOIN
in many systems is the
inner join
(this can be confusing because
join ... on
can seem to imply an ordering, but it is actually the
join
that applies to the table names, and the
on
that applies to the criteria;
join
is itself ambiguous between order-independence and order-dependence).
all subsequent explanations on join types in this article make use of the following two tables. The rows in these tables serve to illustrate the effect of different types of joins and join-predicates. In the following tables,
Department.DepartmentID
is the primary key, while
Employee.DepartmentID
is a foreign key.
Note: The "Marketing" Department currently has no listed employees. Also, employee "John" has not been assigned to any Department yet.
An inner join is the most common join operation used in applications, and represents the default join-type. Inner join creates a new result table by combining column values of two tables (A and B) based upon the join-predicate. The query compares each row of A with each row of B to find all pairs of rows which satisfy the join-predicate. When the join-predicate is satisfied, column values for each matched pair of rows of A and B are combined into a result row. 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 normally use other approaches like a Hash join or a Sort-merge join where possible, since computing the Cartesian product is very inefficient.
SQL specifies two different syntactical ways to express joins: "explicit join notation" and "implicit join notation".
The "explicit join notation" uses the
JOIN
keyword to specify the table to join, and the
ON
keyword to specify the predicates for the join, as in the following example:
SELECT
*
FROM
employee
INNER
JOIN
department
ON
employee
.
DepartmentID
=
department
.
DepartmentID
The "implicit join notation" simply lists the tables for joining (in the
FROM
clause of the
SELECT
statement), using commas to separate them. Thus, it specifies a cross-join, and the
WHERE
clause may apply additional filter-predicates (which function comparably to the join-predicates in the explicit notation).
The following example shows a query which is equivalent to the one from the previous example, but this time written using the implicit join notation:
SELECT
*
FROM
employee
,
department
WHERE
employee
.
DepartmentID
=
department
.
DepartmentID
The queries given in the examples above will join the Employee and Department tables using the DepartmentID column of both tables. Where the DepartmentID of these tables match (i.e. the join-predicate is satisfied), the query will combine the LastName , DepartmentID and DepartmentName columns from the two tables into a result row. Where the DepartmentID does not match, no result row is generated.
Thus the result of the execution of either of the two queries above will be:
Note:
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.
Notice that the employee "John" and the department "Marketing" do not appear in the query execution results. Neither of these has any matching records in the respective other table: "John" has no associated department, and no employee has the department ID 35. Thus, no information on John or on Marketing appears in the joined table. Depending on the desired results, this behavior may be a subtle bug. Outer joins may be used to avoid it.
One can further classify inner joins as equi-joins, as natural joins, or as cross-joins (see below).
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
*
FROM
employee
INNER
JOIN
department
ON
employee
.
DepartmentID
=
department
.
DepartmentID
SQL provides an optional shorthand notation for expressing equi-joins, by way of the
USING
construct (Feature ID F402):
SELECT
*
FROM
employee
INNER
JOIN
department
USING
(
DepartmentID
)
The
USING
construct is more than mere syntactic sugar, however, since the result set differs from the result set of the version with the explicit predicate. Specifically, any columns mentioned in the
USING
list will appear only once, with an unqualified name, rather than once for each table in the join. In the above case, there will be a single
DepartmentID
column and no
employee.DepartmentID
or
department.DepartmentID
.
The
USING
clause is supported by MySQL, Oracle, PostgreSQL, SQLite, DB2/400 and Firebird in version 2.1 or higher.
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....
The above sample query for inner joins can be expressed as a natural join in the following way:
SELECT
*
FROM
employee
NATURAL
JOIN
department
As with the explicit
USING
clause, only one DepartmentID column occurs in the joined table, with no qualifier:
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 where the join-condition is absent from the statement. In other words, a cross join combines every row in B with every row in A. The number of rows in the result set will be the number of rows in A times the number of rows in B.
Thus, 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;
The cross join does not apply any predicate to filter records from the joined table. Programmers can further filter the results of a cross join by using a
WHERE
clause.
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).
(In this case
left
and
right
refer to the two sides of the
JOIN
keyword.)
No implicit join-notation for outer joins exists in standard SQL.
The result of a
left outer join
(or simply
left join
) for table 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
I've been looking at Pivot queries for use with SQL Server 2005 and have come up with the ... as [Event_Year] ,S.SAMPLE_KEY AS [Event_Key] FROM [SAMPLE] AS S INNER JOIN SURVEY ...
High CPU after upgrading to SQL Server 2005 from 2000 due to batch sort ... | |--Nested Loops(Inner Join, OUTER REFERENCES:([C].[bi_Table02Definition], [Expr1111]) WITH ...
Difference between Full Join and Inner Join in SQl Server 2005. Get real Microsoft SQL Server insights and knowledge from Bytes experts.
SQL SERVER – 2005 – Difference Between INTERSECT and INNER JOIN – INTERSECT vs. INNER JOIN. August 3, 2008 by pinaldave
Dynamic PIVOT in SQL Server 2005 The PIVOT operator available in SQL Server 2005 is used to ... inner join northwind..[order details] as od on p.productid=od.productid
INTERSECT operator in SQL Server 2005 is used to retrieve the common records from both the left and the right query of the Intersect Operator. INTERSECT ...
SQL SERVER - 2005 - Difference Between INTERSECT and INNER JOIN - INTERSECT vs. INNER JOIN
This article explains about the views in SQL Server 2005 with sample SQL queries.
Pinal Dave's article about SQL SERVER - 2005 - Difference Between INTERSECT and INNER JOIN - INTERSECT vs. INNER JOIN
... TPNB, sum(AllocatedQty) as 'QTY' from integration.ProductLookup as PL inner join ... Massive CROSS JOIN in SQL Server 2005