Join

What Does Join Mean?

A join is an SQL operation performed to establish a connection between two or more database tables based on matching columns, thereby creating a relationship between the tables. Most complex queries in an SQL database management system involve join commands.

Advertisements

There are different types of joins. The type of join a programmer uses determines which records the query selects. Three algorithms work behind join operations: hash join, sort-merge join and nested loop join.

Techopedia Explains Join

The default join type is the inner join. An inner join selects records from two tables that hold matching values. Records that do not hold matching or common values are excluded from the output. The query compares each row of the first table with rows of the second table to find rows to satisfy the join predicate.

For instance, if one table contains employee details and another contains manager information, a join can be performed on the employee and manager tables to display employees who are also managers. The following query displays employees who are managers:

SELECT * FROM Employee INNER JOIN Manager ON Employee.Managerid = Manager.Managerid

A join is always performed on matching columns, which are specified in the “ON” clause of the query. The matching column in this example is “Managerid”. Since the ‘=’ operator is used, it is called an equijoin.

A natural join also produces the same output but uses a “USING” keyword in the joining clause. The above query can be modified as follows to indicate a natural join:

SELECT employee, manager FROM Employee INNER JOIN Manager USING (Managerid)

Even if a matching column is not specified, a join is still performed between two tables. This type of join is known as a cross join (sometimes called a Cartesian product), which is the simplest form of join. Because a constraint on the key is not specified, every row in the first table is joined with all rows in the second table. If the first table has two rows and the second table has three rows, the output will have six rows.

The outer join is another important join type. Outer joins, in general, take in all records of one table and matching records of the other table as output. An outer join can be either a left outer join or right outer join. In a left outer join, all tables of the left table – even if they do not satisfy the matching conditions – and the matching rows of the right table are displayed in the output. In a right outer join, all rows of the right table and matching rows of the left table are displayed as output.

In rare cases, a table can be joined to itself. This is called a self-join.

Advertisements

Related Terms

Latest Data Management Terms

Related Reading

Margaret Rouse

Margaret Rouse is an award-winning technical writer and teacher known for her ability to explain complex technical subjects to a non-technical, business audience. Over the past twenty years her explanations have appeared on TechTarget websites and she's been cited as an authority in articles by the New York Times, Time Magazine, USA Today, ZDNet, PC Magazine and Discovery Magazine.Margaret's idea of a fun day is helping IT and business professionals learn to speak each other’s highly specialized languages. If you have a suggestion for a new definition or how to improve a technical explanation, please email Margaret or contact her…