Joins
A JOIN clause is used to combine rows from two or more tables, based on a related column between
them.
1. Cross join
2. Inner join
3. Left join
4. Right join
5. Natural join
6. Straight_join
A full join between tables produces all possible combinations of rows
For example, joining a 100-row table to a 200-row table produces a result containing 100 X 200, or
20,000 rows.
In the output column list, you can name columns from any or all of the joined tables, or use
expressions that are based on those columns
table_references:
escaped_table_reference [, escaped_table_reference] ...
escaped_table_reference:
table_reference
| { OJ table_reference }
table_reference:
table_factor
| join_table
table_factor:
table_subquery [AS] alias
| ( table_references )
join_table:
table_reference [INNER | CROSS] JOIN table_factor [join_condition]
| table_reference STRAIGHT_JOIN table_factor
| table_reference STRAIGHT_JOIN table_factor ON conditional_expr
| table_reference {LEFT|RIGHT} [OUTER] JOIN table_reference join_condition
| table_reference NATURAL [{LEFT|RIGHT} [OUTER]] JOIN table_factor
join_condition:
ON conditional_expr
| USING (column_list)
In MySQL, JOIN, CROSS JOIN, and INNER JOIN are syntactic equivalents (they can replace each other).
In standard SQL, they are not equivalent
A join enables you to retrieve records from two (or more) logically related tables in a single result
set.
JOIN clauses are used to return the rows of two or more queries using two or more tables that
shares a meaningful relationship based on a common set of values.
Select t1.col, t2.coln from table1 t1, table2 t2 where t1.co_coln_name= t2.co_coln_name and
[conditions]
Select t1.col, t2.coln from table1 t1 [ inner|left|right|straight|cross|natural ] join table2 t2
on t1.co_coln_name= t2.co_coln_name and [conditions]
Types of MySQL Joins :
Cross join and inner join are same, if you don’t specify any condition then cross, inner, simple join
produce a Cartesian product
Natural join with out condition produce a simple join with only matched columns, matched column
is displayed only once
Left join, right join should be used with conditions
straight_join is used for performance improvement.
On clause
Select * from t1 join t2 on t1.id=t2.id;
The same can be replaced by “Using” clause
Select * from t1 join t2 using (id);
INNER JOIN
MySQL INNER JOIN
The INNER JOIN is such a JOIN in which all rows can be selected from both participating tables as long as there is a match
between the columns. Usage of INNER JOIN combines the tables. An INNER JOIN allows rows from either table to appear in
the result if and only if both tables meet the conditions specified in the ON clause.
LEFT JOIN
MySQL LEFT JOIN
The LEFT JOIN fetches from the table on the left side of the join statement. If a
record returned from the left table has no matching record in the table on the right
side of the join, it is still returned, and the corresponding column from the right table
returns a NULL value.
RIGHT JOIN
MySQL RIGHT JOIN
The RIGHT JOIN is such a join which specifies that all records be fetched from the table on the right side of the join statement,
even if the table on the left has no matching record. In this case, the columns from the left table return NULL values.
STRAIGHT JOIN
CROSS JOIN
MySQL CROSS JOIN
A CROSS JOIN is such a join which specifies the complete cross product of two tables. For each record in the first table, all the
records in the second table are joined, creating a potentially huge result set