An SQL JOIN clause combines records from two or more tables in a database.

[1] 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 JOINs: 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.

Contents
[hide]
• •

• •

• •

• • • •

1 Sample tables 2 Inner join o 2.1 Equi-join o 2.2 Natural join o 2.3 Cross join 3 Outer joins o 3.1 Left outer join o 3.2 Right outer joins o 3.3 Full outer join 4 Self-join o 4.1 Example 5 Merge Rows o 5.1 MySQL o 5.2 PostgreSQL 6 Alternatives 7 Implementation o 7.1 Join algorithms  7.1.1 Nested loops 8 See also 9 Notes 10 References 11 External links

[edit] Sample tables
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 key.

department VALUES (33. employee VALUES ('Smith'. 'Sales'). 'Engineering'). employee VALUES ('Robinson'. DepartmentID int ). ALTER TABLE employee ADD CONSTRAINT fk_employee_dept FOREIGN KEY (DepartmentID) REFERENCES department(DepartmentID). employee "John" has not been assigned to any Department yet. 31). employee VALUES ('Jones'. Script to add data to the tables INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO department VALUES (31. . Also. NULL). 33). employee VALUES ('John'. employee VALUES ('Rafferty'.Employee Table LastName DepartmentID Rafferty 31 Jones 33 Steinberg 33 Robinson 34 Smith 34 NULL John Department Table DepartmentID DepartmentName 31 Sales 33 Engineering 34 Clerical 35 Marketing Note: The "Marketing" Department currently has no listed employees. CREATE TABLE department ( DepartmentID int UNIQUE. 'Clerical'). 34). department VALUES (35. Script to create the tables CREATE TABLE employee ( LastName varchar(25). 33). 34). 'Marketing'). department VALUES (34. employee VALUES ('Steinberg'. DepartmentName varchar(25) ).

The queries given in the examples above will join the Employee and Department tables using the DepartmentID column of both tables. When the join-predicate is satisfied.[edit] Inner join An inner join is the most common join operation used in applications and can be regarded as the default join-type. Thus the result of the execution of either of the two queries above will be: Employee.e. 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. The "explicit join notation" uses the JOIN keyword to specify the table to join. The following example shows a query which is equivalent to the one from the previous example.Departmen Department. it specifies a cross-join. Where the DepartmentID does not match.DepartmentID. The query compares each row of A with each row of B to find all pairs of rows which satisfy the join-predicate. DepartmentID and DepartmentName columns from the two tables into a result row. column values for each matched pair of rows of A and B are combined into a result row. the query will combine the LastName.DepartmentID = department. and the ON keyword to specify the predicates for the join. no result row is generated. using commas to separate them. Inner join creates a new result table by combining column values of two tables (A and B) based upon the join-predicate. Thus.Departm Department.LastN Employee. and the WHERE clause may apply additional filter-predicates (which function comparably to the join-predicates in the explicit notation). department WHERE employee. since computing the Cartesian product is very inefficient. but this time written using the implicit join notation: SELECT * FROM employee. The "implicit join notation" simply lists the tables for joining (in the FROM clause of the SELECT statement).DepartmentID = department. as in the following example: SELECT * FROM employee INNER JOIN department ON employee.DepartmentID. SQL specifies two different syntactical ways to express joins: "explicit join notation" and "implicit join notation".Departm ame entID tName entID . the join-predicate is satisfied). Where the DepartmentID of these tables match (i.

this behavior may be a subtle bug. or as cross-joins. unless the join condition explicitly uses the IS NULL or IS NOT NULL predicates. that uses only equality comparisons in the join-predicate. Outer joins may be used to avoid it. Notice that the employee "John" and the department "Marketing" do not appear in the query execution results.DepartmentID. however. since NULL will never match any other value (or even NULL itself). The USING clause is not supported by SQL Server 2005. The query shown above has already provided an example of an equi-join: SELECT * FROM employee EQUI JOIN department ON employee. since the result set differs from the result set of the version with the explicit predicate. Using other comparison operators (such as <) disqualifies a join as an equi-join. Thus.DepartmentID. or theta join. . no information on John or on Marketing appears in the joined table. One can further classify inner joins as equi-joins. and no employee has the department ID 35. any columns mentioned in the USING list will appear only once. is a specific type of comparator-based join. there will be a single DepartmentID column and no employee. [edit] Equi-join An equi-join. In the above case. rather than once for each table in the join. Specifically. Neither of these has any matching records in the respective other table: "John" has no associated department. 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.DepartmentID = department. Depending on the desired results.DepartmentID or department. also known as an equijoin. as natural joins. SQL provides an optional shorthand notation for expressing equi-joins. with an unqualified name.Robinson Jones Smith Steinberg Rafferty 34 33 34 33 31 Clerical Engineering Clerical Engineering Sales 34 33 34 33 31 Note: Programmers should take special care when joining tables on columns that can contain NULL values.

department. then the cross join is written as A × B. Thus. The SQL code for a cross join lists the tables for joining (FROM). cartesian join or product provides the foundation upon which all types of inner joins operate. if A and B are two sets. The above sample query for inner joins can be expressed as a natural join in the following way: SELECT * FROM employee NATURAL JOIN department. Example of an explicit cross join: SELECT * FROM employee CROSS JOIN department.DepartmentName 34 Smith Clerical 33 Jones Engineering 34 Robinson Clerical 33 Steinberg Engineering 31 Rafferty Sales [edit] Cross join A cross join. The resulting joined table contains only one column for each pair of equally-named columns. Example of an implicit cross join: SELECT * FROM employee. Employee.Departmen Department. it equates to an inner join where the join-condition always evaluates to True or where the join-condition is absent from the statement.Departm . In other words.LastN Employee.Departm Department. The join predicate arises implicitly by comparing all columns in both tables that have the same column-name in the joined tables.LastName Department. As with the explicit USING clause. but does not include any filtering join-predicate. with no qualifier: DepartmentID Employee. only one DepartmentID column occurs in the joined table. a cross join combines every row in B with every row in A.[edit] Natural join A natural join offers a further specialization of equi-joins. Thus. The number of rows in the result set will be the number of rows in A times the number of rows in B. A cross join returns the cartesian product of the sets of records from the two joined tables.

ame Rafferty Jones Steinberg Smith Robinson John Rafferty Jones Steinberg Smith Robinson John Rafferty Jones Steinberg Smith Robinson John Rafferty Jones Steinberg Smith Robinson John entID 31 33 33 34 34 NULL 31 33 33 34 34 NULL 31 33 33 34 34 NULL 31 33 33 34 34 NULL tName Sales Sales Sales Sales Sales Sales Engineering Engineering Engineering Engineering Engineering Engineering Clerical Clerical Clerical Clerical Clerical Clerical Marketing Marketing Marketing Marketing Marketing Marketing entID 31 31 31 31 31 31 33 33 33 33 33 33 34 34 34 34 34 34 35 35 35 35 35 35 The cross join does not apply any predicate to filter records from the joined table. right. depending on which table(s) one retains the rows from (left.) No implicit join-notation for outer joins exists in standard SQL. [edit] Left outer join . Outer joins subdivide further into left outer joins. Programmers can further filter the results of a cross join by using a WHERE clause. and full outer joins. The joined table retains each record—even if no other matching record exists. right outer joins. or both). [edit] Outer joins An outer join does not require each record in the two joined tables to have a matching record. (In this case left and right refer to the two sides of the JOIN keyword.

this allows us to find each employee and his or her department. this allows us to find an employee's department.DepartmentID = department. This means that if the ON clause matches 0 (zero) records in B. Example of a left outer join. For example. the values in the left table will be repeated for each distinct row on the right table. Example right outer join. even if the join-condition does not find any matching record in the "right" table (B). but still show departments that have no employees. where unassigned employees are excluded from the result). Every row from the "right" table (B) will appear in the joined table at least once. This means that a left outer join returns all the values from the left table.Departmen Department. If no matching row from the "left" table (A) exists.DepartmentID.LastN Employee. but still shows the employee(s) even when they have not been assigned to a department (contrary to the inner-join example above. plus matched values from the right table (or NULL in case of no matching join predicate).Departm ame entID tName entID Jones 33 Engineering 33 Rafferty 31 Sales 31 Robinson 34 Clerical 34 Smith 34 Clerical 34 NULL NULL NULL John Steinberg 33 Engineering 33 [edit] Right outer joins A right outer join (or right join) closely resembles a left outer join. For example. with the additional result row italicized: SELECT * FROM employee RIGHT OUTER JOIN department ON employee. with the additional result row italicized: SELECT * FROM employee LEFT OUTER JOIN department ON employee.Departm .Departm Department. Employee. except with the treatment of the tables reversed.LastN Employee. 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). the join will still return a row in the result—but with NULL in each column from B. If the left table returns one row and the right table returns more than one matching row for it. Employee.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).Departm Department.DepartmentID = department. NULL will appear in columns from A for those records that have no match in B.DepartmentID.Departmen Department.

and fill in NULLs for missing matches on either side. For example.DepartmentID. since they can always be replaced with left outer joins (with the table order switched) and provide no additional functionality.Departmen Department. [edit] Full outer join A full outer join combines the results of both left and right outer joins. Employee. this allows us to see each employee who is in a department and each department that has an employee. The result above is produced also with a left outer join: SELECT * FROM department LEFT OUTER JOIN employee ON employee.DepartmentID. explicit right outer joins are rarely used.ame Smith Jones Robinson Steinberg Rafferty NULL entID 34 33 34 33 31 NULL tName Clerical Engineering Clerical Engineering Sales Marketing entID 34 33 34 33 31 35 In practice. but also see each employee who is not part of a department and each department which doesn't have an employee.LastN Employee.DepartmentID = department.DepartmentID = department. but they can emulate it through the use of left and right outer joins and unions. The joined table will contain all records from both tables.Departm Department. The same example can appear as follows: . Example full outer join: SELECT * FROM employee FULL OUTER JOIN department ON employee.Departm ame entID tName entID Smith 34 Clerical 34 Jones 33 Engineering 33 Robinson 34 Clerical 34 NULL NULL NULL John Steinberg 33 Engineering 33 Rafferty 31 Sales 31 NULL NULL Marketing 35 Some database systems (like MySQL) do not support this functionality directly.

DepartmentID = department. so outer join can be emulated as follows: SELECT employee. SQLite does not support right join.DepartmentID IS NULL.DepartmentID = department. If you had 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. However.DepartmentID WHERE employee.[2] This is best illustrated by the following example.DepartmentID = department. you could use a normal join operation to find the answer table.* FROM department LEFT JOIN employee ON employee.DepartmentID UNION SELECT * FROM employee RIGHT JOIN department ON employee. department.* FROM employee LEFT JOIN department ON employee. [edit] Self-join A self-join is joining a table to itself.DepartmentID UNION ALL SELECT employee.DepartmentID = department. department. [edit] Example A query to find all pairings of two employees in the same country is desired.[3] Considering a modified Employee table such as the following: Employee Table 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 NULL 306 John Germany .*.DepartmentID.*. all the employee information is contained within a single large table.SELECT * FROM employee LEFT JOIN department ON employee.

and the topmost and bottommost are of no interest at all in this example.EmployeeID ORDER BY F. note that: • • • • and S are aliases for the first and second copies of the employee table. The example question only wanted pairs of employees in the same country. S.Country = S. F. the following less useful table would be generated (the table below displays only the "Germany" portion of the result): F 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.EmployeeID. [edit] Merge Rows To be able to do a select so as to merge multiple rows into 1 row : "group_concat notation". S.EmployeeID.EmployeeID also excludes duplicate pairings.EmployeeID < S. S.LastName.EmployeeID < S.Country = S. Employee Table after Self-join by Country 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.An example solution query could be as follows: SELECT F.EmployeeID < S. The condition F. Without it. F.Country AND F. Employee S WHERE F.EmployeeID excludes pairings where the EmployeeIDs are the same.EmployeeID. Which results in the following table being generated. F.Country excludes pairings between employees in different countries.Country FROM Employee F.EmployeeID. . The condition F.LastName.

and PostgreSQL group_concat has to be manually added since it doesn't already exist. . error// JOIN SQL CREATE AGGREGATE group_concat ( BASETYPE = text. Smith [edit] MySQL SELECT DepartmentID. [edit] PostgreSQL First the function _group_concat and aggregate group_concat need to be created before that query can be possible. group_concat(LastName) AS LastNames FROM employee GROUP BY DepartmentID. STYPE = text ). CREATE OR REPLACE FUNCTION _group_concat(text. text) RETURNS text AS $$ SELECT CASE WHEN $2 IS NULL THEN $1 WHEN $1 IS NULL THEN $2 ELSE $1 operator(pg_catalog. ' operator(pg_catalog. SFUNC = _group_concat.||) '.||) $2 END $$ IMMUTABLE LANGUAGE SQL. As in the following example: Using the Employee Table: LastName DepartmentID Rafferty 31 Jones 33 Steinberg 33 Robinson 34 Smith 34 NULL John to achieve the following results Table DepartmentID LastNames NULL John 31 Rafferty 33 Jones.MySQL uses the group_concat keyword to achieve that goal. Steinberg 34 Robinson.

[edit] Alternatives The effect of outer joins can also be obtained using correlated subqueries.DepartmentID.DepartmentID ) AS DepartmentName FROM employee.DepartmentID.SELECT DepartmentID.LastName. can also be written as SELECT employee.DepartmentName FROM employee LEFT OUTER JOIN department ON employee.DepartmentID.LastName. (SELECT department.DepartmentID = department. . employee.DepartmentName FROM department WHERE employee. employee. department. group_concat(LastName) AS LastNames FROM employee GROUP BY DepartmentID.DepartmentID = department. For example SELECT employee.

Sign up to vote on this title
UsefulNot useful