Join (SQL)
Encyclopedia
An SQL
join clause combines records from two or more table
s in a database. It creates a set that can be saved as a table or used as is. A
A programmer writes a
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.
to eliminate duplication of information when objects may have one-to-many relationships. For example, a Department may be associated with many different Employees. Joining two tables effectively creates another table which combines information from both tables. This is at some expense in terms of the time it takes to compute the join. While it is also possible to simply maintain such a table if speed is important, duplicate information may take extra space, and add the expense and complexity of maintaining data integrity if data which is duplicated later changes.
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 the
.
Note: The "Marketing" Department currently has no listed employees. Also, employee "John" has not been assigned to any Department yet.
(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
The "implicit join notation" simply lists the tables for joining (in the
The following example shows a query which is equivalent to the one from the previous examples, but this time written using the implicit join notation:
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 (not even NULL itself), unless the join condition explicitly uses the
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.
If columns in an equijoin have the same name, SQL/92 provides an optional shorthand notation for expressing equi-joins, by way of the
The
, however, since the result set differs from the result set of the version with the explicit predicate. Specifically, any columns mentioned in the
The
Most experts agree that NATURAL JOINs are dangerous and therefore strongly discourage their use. The danger comes from inadvertently adding a new column with a name that matches the other table. This means that any existing natural join will start comparing rows with different criteria than before, and will produce different results on the same data.
The above sample query for inner joins can be expressed as a natural join in the following way:
As with the explicit
PostgreSQL, MySQL and Oracle support natural joins, but not Microsoft T-SQL. The columns used in the join are implicit so the join code does not show which columns are expected, and a change in column names may change the results. An INNER JOIN performed on 2 tables having the same field name has the same effect.
of rows from tables in the join. In other words, it will produce rows which combine each row from the first table with each row from the second table.
Example of an explicit cross join:
Example of an implicit cross join:
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
(In this case left and right refer to the two sides of the
No implicit join-notation for outer joins exists in standard SQL.
From Oracle 9i onwards the LEFT OUTER JOIN statement can be used as well as (+).
For example, this allows us to find an employee's department, but still shows the employee(s) even when they have not been assigned to a department (contrary to the inner-join example above, where unassigned employees are excluded from the result).
Example of a left outer join, with the additional result row italicized:
Oracle 8i and lower supports the alternate syntax:
Sybase supports the alternate syntax:
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 his or her department, but still show departments that have no employees.
Below is shown an example of right outer join, with the additional result row italicized:
In practice, explicit right outer joins are rarely used, since they can always be replaced with left outer joins (with the table order switched) and provide no additional functionality. The result above is produced also with a left outer join:
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 which doesn't have an employee.
Example full outer join:
Some database systems do not support the full outer join functionality directly, but they can emulate it through the use of an inner join and UNION ALL selects of the "single table rows" from left and right tables respectively. The same example can appear as follows:
Consider a modified
An example solution query could be as follows:
Which results in the following table being generated.
For this example:
Only one of the two middle pairings is needed to satisfy the original question, and the topmost and bottommost are of no interest at all in this example.
MySQL
uses the
9.0 has the
array_to_string(array_agg(value),', ')
or the creation of an aggregate function.
As for version 9.0:
Then you can use the following query:
From version 2005, one can accomplish this task using FOR XML PATH:
can also be written as
determines how to execute a query containing joins. A query optimizer has two basic freedoms:
Many join-algorithms treat their inputs differently. One can refer to the inputs to a join as the "outer" and "inner" join operands, or "left" and "right", respectively. In the case of nested loops, for example, the database system will scan the entire inner relation for each row of the outer relation.
One can classify query-plans involving joins as follows:
left-deep : using a base table (rather than another join) as the inner operand of each join in the plan
right-deep : using a base table as the outer operand of each join in the plan
bushy : neither left-deep nor right-deep; both inputs to a join may themselves result from joins
These names derive from the appearance of the query plan
if drawn as a tree, with the outer join relation on the left and the inner relation on the right (as convention dictates).
, Sort-merge join
and Hash join
.
SQL
SQL is a programming language designed for managing data in relational database management systems ....
join clause combines records from two or more table
Table (database)
In relational databases and flat file databases, a table is a set of data elements that is organized using a model of vertical columns and horizontal rows. A table has a specified number of columns, but can have any number of rows...
s 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
. As a special case, a table (base table, view, or joined table) can JOIN
to itself in a self-join.A programmer writes a
JOIN
predicateBranch predication
Branch predication is a strategy in computer architecture design for mitigating the costs usually associated with conditional branches, particularly branches to short sections of code...
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.
Sample tables
Relational databases are often normalizedDatabase normalization
In the design of a relational database management system , the process of organizing data to minimize redundancy is called normalization. The goal of database normalization is to decompose relations with anomalies in order to produce smaller, well-structured relations...
to eliminate duplication of information when objects may have one-to-many relationships. For example, a Department may be associated with many different Employees. Joining two tables effectively creates another table which combines information from both tables. This is at some expense in terms of the time it takes to compute the join. While it is also possible to simply maintain such a table if speed is important, duplicate information may take extra space, and add the expense and complexity of maintaining data integrity if data which is duplicated later changes.
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 the
DepartmentID
column of the Department
table (which can be designated as Department.DepartmentID
) is the primary key, while Employee.DepartmentID
is a foreign keyForeign key
In the context of relational databases, a foreign key is a referential constraint between two tables.A foreign key is a field in a relational table that matches a candidate key of another table...
.
LastName | DepartmentID |
---|---|
Rafferty | 31 |
Jones | 33 |
Steinberg | 33 |
Robinson | 34 |
Smith | 34 |
John |
DepartmentID | DepartmentName |
---|---|
31 | Sales |
33 | Engineering |
34 | Clerical |
35 | Marketing |
Note: The "Marketing" Department currently has no listed employees. Also, employee "John" has not been assigned to any Department yet.
Inner join
An inner join is the most common join operation used in applications and can be regarded as 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 productCartesian product
In mathematics, a Cartesian product is a construction to build a new set out of a number of given sets. Each member of the Cartesian product corresponds to the selection of one element each in every one of those sets...
(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
Hash join
The Hash join is an example of a join algorithm and is used in the implementation of a relational database management system.The task of a join algorithm is to find, for each distinct value of the join attribute, the set of tuples in each relation which have that value.Hash joins require an...
or a sort-merge join
Sort-merge join
The Sort-Merge Join is an example of a join algorithm and is used in the implementation of a relational database management system....
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: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 examples, but this time written using the implicit join notation:
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:
Employee.LastName | Employee.DepartmentID | Department.DepartmentName | Department.DepartmentID |
---|---|---|---|
Robinson | 34 | Clerical | 34 |
Jones | 33 | Engineering | 33 |
Smith | 34 | Clerical | 34 |
Steinberg | 33 | Engineering | 33 |
Rafferty | 31 | Sales | 31 |
Note: Programmers should take special care when joining tables on columns that can contain NULL
Null (SQL)
Null is a special marker used in Structured Query Language to indicate that a data value does not exist in the database. Introduced by the creator of the relational database model, E. F. Codd, SQL Null serves to fulfill the requirement that all true relational database management systems support...
values, since NULL will never match any other value (not 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.
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:If columns in an equijoin have the same name, SQL/92 provides an optional shorthand notation for expressing equi-joins, by way of the
USING
construct:The
USING
construct is more than mere syntactic sugarSyntactic sugar
Syntactic sugar is a computer science term that refers to syntax within a programming language that is designed to make things easier to read or to express....
, 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 not supported by SQL Server and Sybase.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-names in the joined tables. The resulting joined table contains only one column for each pair of equally-named columns.Most experts agree that NATURAL JOINs are dangerous and therefore strongly discourage their use. The danger comes from inadvertently adding a new column with a name that matches the other table. This means that any existing natural join will start comparing rows with different criteria than before, and will produce different results on the same data.
The above sample query for inner joins can be expressed as a natural join in the following way:
As with the explicit
USING
clause, only one DepartmentID column occurs in the joined table, with no qualifier:DepartmentID | Employee.LastName | Department.DepartmentName |
---|---|---|
34 | Smith | Clerical |
33 | Jones | Engineering |
34 | Robinson | Clerical |
33 | Steinberg | Engineering |
31 | Rafferty | Sales |
PostgreSQL, MySQL and Oracle support natural joins, but not Microsoft T-SQL. The columns used in the join are implicit so the join code does not show which columns are expected, and a change in column names may change the results. An INNER JOIN performed on 2 tables having the same field name has the same effect.
Cross join
CROSS JOIN returns the Cartesian productCartesian product
In mathematics, a Cartesian product is a construction to build a new set out of a number of given sets. Each member of the Cartesian product corresponds to the selection of one element each in every one of those sets...
of rows from tables in the join. In other words, it will produce rows which combine each row from the first table with each row from the second table.
Example of an explicit cross join:
Example of an implicit cross join:
Employee.LastName | Employee.DepartmentID | Department.DepartmentName | Department.DepartmentID |
---|---|---|---|
Rafferty | 31 | Sales | 31 |
Jones | 33 | Sales | 31 |
Steinberg | 33 | Sales | 31 |
Smith | 34 | Sales | 31 |
Robinson | 34 | Sales | 31 |
John | Sales | 31 | |
Rafferty | 31 | Engineering | 33 |
Jones | 33 | Engineering | 33 |
Steinberg | 33 | Engineering | 33 |
Smith | 34 | Engineering | 33 |
Robinson | 34 | Engineering | 33 |
John | Engineering | 33 | |
Rafferty | 31 | Clerical | 34 |
Jones | 33 | Clerical | 34 |
Steinberg | 33 | Clerical | 34 |
Smith | 34 | Clerical | 34 |
Robinson | 34 | Clerical | 34 |
John | Clerical | 34 | |
Rafferty | 31 | Marketing | 35 |
Jones | 33 | Marketing | 35 |
Steinberg | 33 | Marketing | 35 |
Smith | 34 | Marketing | 35 |
Robinson | 34 | Marketing | 35 |
John | Marketing | 35 |
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.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).(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.
Left outer join
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 theON
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). If the right table returns one row and the left table returns more than one matching row for it, the values in the right table will be repeated for each distinct row on the left table.From Oracle 9i onwards the LEFT OUTER JOIN statement can be used as well as (+).
For example, this allows us to find an employee's department, but still shows the employee(s) even when they have not been assigned to a department (contrary to the inner-join example above, where unassigned employees are excluded from the result).
Example of a left outer join, with the additional result row italicized:
Employee.LastName | Employee.DepartmentID | Department.DepartmentName | Department.DepartmentID |
---|---|---|---|
Jones | 33 | Engineering | 33 |
Rafferty | 31 | Sales | 31 |
Robinson | 34 | Clerical | 34 |
Smith | 34 | Clerical | 34 |
John | |||
Steinberg | 33 | Engineering | 33 |
Oracle 8i and lower supports the alternate syntax:
Sybase supports the alternate syntax:
Right outer join
A right outer join (or right join) closely resembles a left outer join, except with the treatment of the tables reversed. Every row 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 B.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 his or her department, but still show departments that have no employees.
Below is shown an example of right outer join, with the additional result row italicized:
Employee.LastName | Employee.DepartmentID | Department.DepartmentName | Department.DepartmentID |
---|---|---|---|
Smith | 34 | Clerical | 34 |
Jones | 33 | Engineering | 33 |
Robinson | 34 | Clerical | 34 |
Steinberg | 33 | Engineering | 33 |
Rafferty | 31 | Sales | 31 |
Marketing | 35 |
In practice, explicit right outer joins are rarely used, since they can always be replaced with left outer joins (with the table order switched) and provide no additional functionality. The result above is produced also with a left outer join:
Full outer join
Conceptually, a full outer join combines the effect of applying both left and right outer joins. Where records in the FULL OUTER JOINed tables do not match, the result set will have NULL values for every column of the table that lacks a matching row. For those records that do match, a single row will be produced in the result set (containing fields populated from both tables).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 which doesn't have an employee.
Example full outer join:
Employee.LastName | Employee.DepartmentID | Department.DepartmentName | Department.DepartmentID |
---|---|---|---|
Smith | 34 | Clerical | 34 |
Jones | 33 | Engineering | 33 |
Robinson | 34 | Clerical | 34 |
John | |||
Steinberg | 33 | Engineering | 33 |
Rafferty | 31 | Sales | 31 |
Marketing | 35 |
Some database systems do not support the full outer join functionality directly, but they can emulate it through the use of an inner join and UNION ALL selects of the "single table rows" from left and right tables respectively. The same example can appear as follows:
Example
A query to find all pairings of two employees in the same country is desired. If there were two separate tables for employees and a query which requested employees in the first table having the same country as employees in the second table, a normal join operation could be used to find the answer table. However, all the employee information is contained within a single large table.Consider a modified
Employee
table such as the following:
EmployeeID | LastName | Country | DepartmentID |
---|---|---|---|
123 | Rafferty | Australia | 31 |
124 | Jones | Australia | 33 |
145 | Steinberg | Australia | 33 |
201 | Robinson | United States | 34 |
305 | Smith | Germany | 34 |
306 | John | Germany |
An example solution query could be as follows:
Which results in the following table being generated.
EmployeeID | LastName | EmployeeID | LastName | Country |
---|---|---|---|---|
123 | Rafferty | 124 | Jones | Australia |
123 | Rafferty | 145 | Steinberg | Australia |
124 | Jones | 145 | Steinberg | Australia |
305 | Smith | 306 | John | Germany |
For this example:
F
andS
are aliasesAlias (SQL)An alias is a feature of SQL that is supported by most, if not all, relational database management systems .Aliases provide Database Administrators , as well as other database users, with two things:...
for the first and second copies of the employee table.- The condition
F.Country = S.Country
excludes pairings between employees in different countries. The example question only wanted pairs of employees in the same country. - The condition
F.EmployeeID < S.EmployeeID
excludes pairings where theEmployeeID
of the first employee is less than theEmployeeID
of the second employee. In other words, the effect of this condition is to exclude duplicate pairings and self-pairings. Without it, the following less useful table would be generated (the table below displays only the "Germany" portion of the result):EmployeeID LastName EmployeeID LastName Country 305 Smith 305 Smith Germany 305 Smith 306 John Germany 306 John 305 Smith Germany 306 John 306 John Germany
Only one of the two middle pairings is needed to satisfy the original question, and the topmost and bottommost are of no interest at all in this example.
Merge rows
To be able to do a select so as to merge multiple rows into 1 row : "group_concat notation".MySQL
MySQL
MySQL officially, but also commonly "My Sequel") is a relational database management system that runs as a server providing multi-user access to a number of databases. It is named after developer Michael Widenius' daughter, My...
uses the
group_concat
keyword to achieve that goal, and PostgreSQLPostgreSQL
PostgreSQL, often simply Postgres, is an object-relational database management system available for many platforms including Linux, FreeBSD, Solaris, MS Windows and Mac OS X. It is released under the PostgreSQL License, which is an MIT-style license, and is thus free and open source software...
9.0 has the
string_agg
function. Versions before 9.0 required the use of something likearray_to_string(array_agg(value),', ')
or the creation of an aggregate function.
LastName | DepartmentID |
---|---|
Rafferty | 31 |
Jones | 33 |
Steinberg | 33 |
Robinson | 34 |
Smith | 34 |
John |
DepartmentID | LastNames |
---|---|
John | |
31 | Rafferty |
33 | Jones, Steinberg |
34 | Robinson, Smith |
MySQL
PostgreSQL
First the function _group_concat and aggregate group_concat need to be created before that query can be possible.As for version 9.0:
Microsoft T-SQL
For versions prior to Microsoft SQL Server 2005, the function group_concat must be created as a user-defined aggregate function before that query can be possible, shown here in C#.Then you can use the following query:
From version 2005, one can accomplish this task using FOR XML PATH:
Alternatives
The effect of an outer join can also be obtained using a UNION ALL between an INNER JOIN and a SELECT of the rows in the "main" table that do not fulfill the join condition. For examplecan also be written as
Implementation
Much work in database-systems has aimed at efficient implementation of joins, because relational systems commonly call for joins, yet face difficulties in optimising their efficient execution. The problem arises because inner joins operate both commutatively and associatively. In practice, this means that the user merely supplies the list of tables for joining and the join conditions to use, and the database system has the task of determining the most efficient way to perform the operation. A query optimizerQuery optimizer
The query optimizer is the component of a database management system that attempts to determine the most efficient way to execute a query. The optimizer considers the possible query plans for a given input query, and attempts to determine which of those plans will be the most efficient...
determines how to execute a query containing joins. A query optimizer has two basic freedoms:
- Join order: Because it joins functions commutatively and associatively, the order in which the system joins tables does not change the final result-set of the query. However, join-order does have an enormous impact on the cost of the join operation, so choosing the best join order becomes very important.
- Join method: Given two tables and a join condition, multiple algorithmAlgorithmIn mathematics and computer science, an algorithm is an effective method expressed as a finite list of well-defined instructions for calculating a function. Algorithms are used for calculation, data processing, and automated reasoning...
s can produce the result-set of the join. Which algorithm runs most efficiently depends on the sizes of the input tables, the number of rows from each table that match the join condition, and the operations required by the rest of the query.
Many join-algorithms treat their inputs differently. One can refer to the inputs to a join as the "outer" and "inner" join operands, or "left" and "right", respectively. In the case of nested loops, for example, the database system will scan the entire inner relation for each row of the outer relation.
One can classify query-plans involving joins as follows:
left-deep : using a base table (rather than another join) as the inner operand of each join in the plan
right-deep : using a base table as the outer operand of each join in the plan
bushy : neither left-deep nor right-deep; both inputs to a join may themselves result from joins
These names derive from the appearance of the query plan
Query plan
A query plan is an ordered set of steps used to access or modify information in a SQL relational database management system. This is a specific case of the relational model concept of access plans....
if drawn as a tree, with the outer join relation on the left and the inner relation on the right (as convention dictates).
Join algorithms
Three fundamental algorithms for performing a join operation are known: Nested loop joinNested loop join
A nested loop join is a naive algorithm that joins two relations R and S by making two nested loops: For each tuple r in R do For each tuple s in S do If r and s satisfy the join condition Then output the tuple...
, Sort-merge join
Sort-merge join
The Sort-Merge Join is an example of a join algorithm and is used in the implementation of a relational database management system....
and Hash join
Hash join
The Hash join is an example of a join algorithm and is used in the implementation of a relational database management system.The task of a join algorithm is to find, for each distinct value of the join attribute, the set of tuples in each relation which have that value.Hash joins require an...
.
External links
- Specific to products
- General