Home »

Join (sql)

The meaning of «join (sql)»

A join clause in SQL – corresponding to a join operation in relational algebra – combines columns from one or more tables into a new table. ANSI-standard SQL specifies five types of JOIN: INNER, LEFT OUTER, RIGHT OUTER, FULL OUTER and CROSS.

To explain join types, the rest of this article uses the following tables:

Department.DepartmentID is the primary key of the Department table, whereas Employee.DepartmentID is a foreign key.

Note that in Employee, "Williams" has not yet been assigned to a department. Also, no employees have been assigned to the "Marketing" department.

This is the SQL statement to create the above tables:

CREATE TABLE department( DepartmentID INT PRIMARY KEY NOT NULL, DepartmentName VARCHAR(20) ); CREATE TABLE employee ( LastName VARCHAR(20), DepartmentID INT REFERENCES department(DepartmentID) ); INSERT INTO department VALUES (31, 'Sales'), (33, 'Engineering'), (34, 'Clerical'), (35, 'Marketing'); INSERT INTO employee VALUES ('Rafferty', 31), ('Jones', 33), ('Heisenberg', 33), ('Robinson', 34), ('Smith', 34), ('Williams', NULL); Cross join[edit]

CROSS JOIN returns the Cartesian product 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. [1]

SELECT * FROM employee CROSS JOIN department;

Example of an implicit cross join:

SELECT * FROM employee, department;

The cross join can be replaced with an inner join with an always-true condition:

SELECT * FROM employee INNER JOIN department ON 1=1;

CROSS JOIN does not itself apply any predicate to filter rows from the joined table. The results of a CROSS JOIN can be filtered using a WHERE clause, which may then produce the equivalent of an inner join.

In the SQL:2011 standard, cross joins are part of the optional F401, "Extended joined table", package.

Normal uses are for checking the server's performance.[why?]

An inner join requires each row in the two joined tables to have matching column values, and is a commonly used join operation in applications but should not be assumed to be the best choice in all situations. 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 that satisfy the join-predicate. When the join-predicate is satisfied by matching non-NULL values, 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 rows in the tables (combining every row in table A with every row in table B) and then returning all rows that satisfy the join predicate. Actual SQL implementations normally use other approaches, such as hash joins or sort-merge joins, since computing the Cartesian product is slower and would often require a prohibitively large amount of memory to store.

Related Searches

Join selection factor
© 2015-2021, Wikiwordbook.info
Copying information without reference to the source is prohibited!
contact us mobile version