Join Tables

Here are the different types of the JOINs in SQL:

  • (INNER) JOIN: Returns records that have matching values in both tables

  • LEFT (OUTER) JOIN: Return all records from the left table, and the matched records from the right table

  • RIGHT (OUTER) JOIN: Return all records from the right table, and the matched records from the left table

  • FULL (OUTER) JOIN: Return all records when there is a match in either left or right table

Caution: Performance Considerations

DBMSs process joins at run-time relating each table as specified. This process can become very resource intensive so be careful not to join tables unnecessarily. The more tables you join the more performance will degrade.

Inner Join

Left/Right Outer Join

An outer join is the join that includes table rows that have no associated rows in the related table.

There are always two basic forms of outer joins—the left outer join and the right outer join. The only difference between them is the order of the tables that they are relating. In other words, a left outer join can be turned into a right outer join simply by reversing the order of the tables in the FROM or WHERE clause. When using OUTER JOIN syntax you must use the RIGHT or LEFT keywords to specify the table from which to include all rows.

Full Outer Join

The FULL OUTER JOIN keyword return all records when there is a match in either left (table1) or right (table2) table records.

Note: FULL OUTER JOIN can potentially return very large result-sets!

Note that MySQL doesn't support FULL OUTER JOIN .

Self Join

A self join is a join of a table to itself. This table appears twice in the FROM clause and is followed by table aliases that qualify column names in the join condition.

Self-joins are often used to replace statements using subqueries that retrieve data from the same table as the outer statement.

Example: Suppose you wanted to send a mailing to all the customers whose names are the same as customers whose contact is "Jim Jones".

Remember that when use self join, we need to give aliases for the two same tables. When we retrieve any column in the table, we need to specify the aliases for the table.

Cross Join

Cross join returns the cartesian product of the two tables. There is no condition or clause as each row of the table1 is joined to each row the table2.

Natural Join

Whenever tables are joined, at least one column will appear in more than one table (the columns being used to create the join). Standard joins return all data, even multiple occurrences of the same column. A natural join simply eliminates those multiple occurrences so that only one of each column is returned.

How does it do this? The answer is it doesn’t—you do it. A natural join is a join in which you select only columns that are unique. This is typically done using a wildcard (SELECT *) for one table and explicit subsets of the columns for all other tables.

Equijoin:

Equijoin works the same as inner join. Per the ANSI SQL specification, use of the INNER JOIN syntax is preferred over the simple equijoins syntax used previously.

To create a equijoin, specify all the tables to be included in FROM clause and how they are related to each other in WHERE clause. When you join two tables, what you are actually doing is pairing every row in the first table with every row in the second table.

Caution: You must use the fully qualified column name (table name and column name separated by a period) whenever there is a possible ambiguity about which column you are referring to.

Join with several conditions:

We can use various filter functions in ON caluses to specify several joining conditions.

Here is a example for self joining temperature table with yesterday's data when yesterday's temperature is lower.

Last updated