Join (SQL)

A join combines records from two or more tables in a relational database. In the Structured Query Language (SQL), there are two types of joins: "inner" and "outer". Outer joins are subdivided further into left outer joins, right outer joins, and full outer joins.

Contents

Examples

A join is essentially a cartesian product followed by a predicate to filter the results. For example, given employee and department tables as follows:

Table "employee"
LastName DepartmentID
Smith 34
Jones 33
Robinson 34
Jasper 36
Steinberg 33
Rafferty 31
Table "department"
DepartmentName DepartmentID
Sales 31
Engineering 33
Clerical 34
Marketing 35

The cartesian product of these two tables is computed using the following SELECT statement:

SELECT * 
FROM employee
    ,department;

An example of a join between these two tables is:

SELECT *
FROM employee
    ,department 
WHERE employee.DepartmentID = department.DepartmentID;

Another way to write this query is to use an explicit JOIN clause, as follows:

SELECT *
FROM employee 
     JOIN 
     department 
       ON employee.DepartmentID = department.DepartmentID

Inner and outer joins

Inner join

This is the default join method if nothing else is specified. An inner join essentially finds the intersection between the two tables. The join example above takes all the records from table A (in this case, employee) and finds the matching record(s) from table B (department). If no match is found, the record from A is not included in the results. If multiple results are found in B that match the predicate then one row will be returned for each (the values from A will be repeated).

Special care must be taken when joining tables on columns that can be NULL since NULL values will never match each other. See Left Outer Join or Right Outer Join for a solution.

SELECT *
FROM employee 
     JOIN 
     department 
       ON employee.DepartmentID = department.DepartmentID
LastName DepartmentID DepartmentName
Smith 34 Clerical
Jones 33 Engineering
Robinson 34 Clerical
Steinberg 33 Engineering
Rafferty 31 Sales

See also Inner join

Left outer join

A left outer join is very different from a inner join. Instead of limiting results to those in both tables, it limits results to those in the "left" table (A). This means that if the ON clause matches 0 records in B, a row in the result will still be returned—but with NULL values for each column from B.

For example, this allows us to find the employee's departments, but still show the employee even when their department is NULL or does not exist. The example above would have ignored employees in non-existent departments.

SELECT *
FROM employee 
     LEFT OUTER JOIN 
     department 
       ON employee.DepartmentID = department.DepartmentID
LastName DepartmentID DepartmentName
Smith 34 Clerical
Jones 33 Engineering
Robinson 34 Clerical
Jasper 36 NULL
Steinberg 33 Engineering
Rafferty 31 Sales

Right outer join

A right outer join is much like a left outer join, except that the tables are reversed. Every record from the right side, B or department, will be returned, and NULL values will be returned for those that have no matching record in A.

SELECT *
FROM employee 
     RIGHT OUTER JOIN 
     department 
       ON employee.DepartmentID = department.DepartmentID
LastName DepartmentID DepartmentName
Smith 34 Clerical
Jones 33 Engineering
Robinson 34 Clerical
Steinberg 33 Engineering
Rafferty 31 Sales
NULL 35 Marketing

Full outer join

Full outer joins are the combination of left and right outer joins. These joins will show records from both tables, and fill in NULLs for missing matches on either side.

Some database systems do not offer this functionality, but it can be emulated through the use of left outer joins and unions.

SELECT *
FROM employee 
     FULL OUTER JOIN 
     department 
       ON employee.DepartmentID = department.DepartmentID
LastName DepartmentID DepartmentName
Smith 34 Clerical
Jones 33 Engineering
Robinson 34 Clerical
Jasper 36 NULL
Steinberg 33 Engineering
Rafferty 31 Sales
NULL 35 Marketing

Implementation

The efficient implementation of joins has been the goal of much work in database systems, because joins are both extremely common but rather difficult to execute efficiently. The difficulty results from the fact that joins are both commutative and associative. In practice, this means that the user merely supplies the list of tables to be joined and the join conditions to be used, and the database system has the task of determining the most efficient way to perform the operation. Determining how to execute a query containing joins is done by the query optimizer. It has two basic freedoms:

  1. join order: because joins are commutative, the order in which tables are joined 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 right join order is very important.
  1. join method: given two tables and a join condition, there are multiple algorithms to produce the result set of the join. Which algorithm is most efficient 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 input tables differently. The input tables are referred to as the outer and inner tables , or left and right, respectively. In the case of nested loops, for example, the entire inner table will be scanned for each row of the outer table .

Query plans involving joins can be classified as:

  • left-deep: the inner table of each join in the plan is a base table (rather than another join).
  • right-deep: the outer table of each join in the plan is a base table.
  • bushy: neither left-deep nor right-deep; both input tables of a join query may be joins themselves.

These names are derived from the appearance of the query if drawn as a tree, with the outer join relation on the left and the inner table on the right (as is the convention).

Join Optimization

See Query optimizer

Join algorithms

There are three fundamental algorithms to perform a join operation.

Nested loops

This is the simplest join algorithm. For each tuple in the outer join relation, the entire inner join relation is scanned, and any tuples that match the join condition are added to the result set. Naturally, this algorithm performs poorly if either the inner or outer join relation is very large.

A refinement to this technique is called "block nested loops": for every block in the outer relation, the entire inner relation is scanned. For each match between the current inner tuple and one of the tuples in the current block of the outer relation, a tuple is added to the join result set. This variant means that more computation is done for each tuple of the inner relation, but far fewer scans of the inner relation are required.

Merge join

If both join relations are sorted by the join attribute, the join can be performed trivially:

  1. For each tuple in the outer relation,
    1. Consider the current "group" of tuples from the inner relation; a group consists of a set of contiguous tuples in the inner relation with the same value in the join attribute.
    2. For each matching tuple in the current inner group, add a tuple to the join result. Once the inner group has been exhausted, both the inner and outer scans can be advanced to the next group.

This is one reason why many optimizers keep track of the sort order of query nodes — if one or both input relations to a merge join is already sorted on the join attribute, an additional sort is not required. Otherwise, the DBMS will need to perform the sort, usually using an external sort to avoid consuming too much memory.

See also Sort-Merge Join

Hash join

See Hash Join

Semi join

A semi join is an efficient join method where first the join attributes of one table are collected and reported to the second one. It was first reported in 1981. It can be improved with a Bloom-Filter (hashing).

See also

Navigation

  • Art and Cultures
    • Art (https://academickids.com/encyclopedia/index.php/Art)
    • Architecture (https://academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (https://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (https://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools