Also how do LEFT JOIN
, RIGHT JOIN
and FULL JOIN
fit in?
相关问题
- SQL join to get the cartesian product of 2 columns
- SQL join to get the cartesian product of 2 columns
- sql execution latency when assign to a variable
- Difference between Types.INTEGER and Types.NULL in
- php PDO::FETCH_ASSOC doesnt detect select after ba
Inner join - An inner join using either of the equivalent queries gives the intersection of the two tables, i.e. the two rows they have in common.
Left outer join - A left outer join will give all rows in A, plus any common rows in B.
Full outer join - A full outer join will give you the union of A and B, i.e. All the rows in A and all the rows in B. If something in A doesn't have a corresponding datum in B, then the B portion is null, and vice versay
Assuming you're joining on columns with no duplicates, which is a very common case:
An inner join of A and B gives the result of A intersect B, i.e. the inner part of a Venn diagram intersection.
An outer join of A and B gives the results of A union B, i.e. the outer parts of a Venn diagram union.
Examples
Suppose you have two tables, with a single column each, and data as follows:
Note that (1,2) are unique to A, (3,4) are common, and (5,6) are unique to B.
Inner join
An inner join using either of the equivalent queries gives the intersection of the two tables, i.e. the two rows they have in common.
Left outer join
A left outer join will give all rows in A, plus any common rows in B.
Right outer join
A right outer join will give all rows in B, plus any common rows in A.
Full outer join
A full outer join will give you the union of A and B, i.e. all the rows in A and all the rows in B. If something in A doesn't have a corresponding datum in B, then the B portion is null, and vice versa.
In simple words:
An inner join retrieve the matched rows only.
Whereas an outer join retrieve the matched rows from one table and all rows in other table ....the result depends on which one you are using:
Left: Matched rows in the right table and all rows in the left table
Right: Matched rows in the left table and all rows in the right table or
Full: All rows in all tables. It doesn't matter if there is a match or not
INNER JOIN
requires there is at least a match in comparing the two tables. For example, table A and table B which implies A ٨ B (A intersection B).LEFT OUTER JOIN
andLEFT JOIN
are the same. It gives all the records matching in both tables and all possibilities of the left table.Similarly,
RIGHT OUTER JOIN
andRIGHT JOIN
are the same. It gives all the records matching in both tables and all possibilities of the right table.FULL JOIN
is the combination ofLEFT OUTER JOIN
andRIGHT OUTER JOIN
without duplication.In Simple Terms,
1.INNER JOIN OR EQUI JOIN : Returns the resultset that matches only the condition in both the tables.
2.OUTER JOIN : Returns the resultset of all the values from both the tables even if there is condition match or not.
3.LEFT JOIN : Returns the resultset of all the values from left table and only rows that match the condition in right table.
4.RIGHT JOIN : Returns the resultset of all the values from right table and only rows that match the condition in left table.
5.FULL JOIN : Full Join and Full outer Join are same.
Inner Join
Retrieve the matched rows only, that is,
A intersect B
.Left Outer Join
Select all records from the first table, and any records in the second table that match the joined keys.
Full Outer Join
Select all records from the second table, and any records in the first table that match the joined keys.
References
Inner and outer joins SQL examples and the Join block
SQL: JOINS