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.
A join is combining the rows from two tables. An inner join attempts to match up the two tables based on the criteria you specify in the query, and only returns the rows that match. If a row from the first table in the join matches two rows in the second table, then two rows will be returned in the results. If there’s a row in the first table that doesn’t match a row in the second, it’s not returned; likewise, if there’s a row in the second table that doesn’t match a row in the first, it’s not returned.
Outer Join.
A left join attempts to find match up the rows from the first table to rows in the second table. If it can’t find a match, it will return the columns from the first table and leave the columns from the second table blank (null).
Having criticized the much-loved red-shaded Venn diagram, I thought it only fair to post my own attempt.
Although @Martin Smith's answer is the best of this bunch by a long way, his only shows the key column from each table, whereas I think ideally non-key columns should also be shown.
The best I could do in the half hour allowed, I still don't think it adequately shows that the nulls are there due to absence of key values in
TableB
or thatOUTER JOIN
is actually a union rather than a join:left join on
(akaleft outer join on
) returnsinner join on
rowsunion all
unmatched left table rows extended by nulls.right join (on
akaright outer join on
) returnsinner join on
rowsunion all
unmatched right table rows extended by nulls.full join on
(akafull outer join on
) returnsinner join on
rowsunion all
unmatched left table rows extended by nullsunion all
unmatched right table rows extended by nulls.(SQL Standard 2006 SQL/Foundation 7.7 Syntax Rules 1, General Rules 1 b, 3 c & d, 5 b.)
So don't
outer join
until you know what underlyinginner join
is involved.Find out what rows
inner join
returns.Read my comments there re the many confused & poor answers.
Then read my comments here re the many confused & poor answers.
A inner join only shows rows if there is a matching record on the other (right) side of the join.
A (left) outer join shows rows for each record on the left hand side, even if there are no matching rows on the other (right) side of the join. If there is no matching row, the columns for the other (right) side would show NULLs.
The answer is in the meaning of each one, so in the results.
My answer is based on above Note.
When you have two tables like these:
CROSS JOIN / OUTER JOIN :
You can have all of those tables data with
CROSS JOIN
or just with,
like this:INNER JOIN :
When you want to add a filter to above results based on a relation like
table1.id = table2.id
you can useINNER JOIN
:LEFT [OUTER] JOIN :
When you want to have all rows of one of tables in the above result -with same relation- you can use
LEFT JOIN
:(For RIGHT JOIN just change place of tables)
FULL OUTER JOIN :
When you also want to have all rows of the other table in your results you can use
FULL OUTER JOIN
:Well, as your need you choose each one that covers your need ;).
I don't see much details about performance and optimizer in the other answers.
Sometimes it is good to know that only
INNER JOIN
is associative which means the optimizer has the most option to play with it. It can reorder the join order to make it faster keeping the same result. The optimizer can use the most join modes.Generally it is a good practice to try to use
INNER JOIN
instead of the different kind of joins. (Of course if it is possible considering the expected result set.)There are a couple of good examples and explanation here about this strange associative behavior: