7
I have a little knowledge in relationship tables, read the answers to the question about What is the difference between INNER JOIN and OUTER JOIN?, and understood, theoretically.
LEFT JOIN
Returns all records from the left table (table A) and any matches with the right table (table B).
RIGHT JOIN
Return all records from the right table (table B) and any matches with the left table (table A).
Using the example below:
Exemplo de LEFT JOIN
SELECT *
FROM tabela_a a
LEFT JOIN tabela_b b
ON b.nome = a.nome;
Exemplo de RIGHT JOIN
SELECT *
FROM tabela_b b
RIGHT JOIN tabela_a a
ON a.nome = b.nome;
Both return:
| nome |
|--------|
| Maria |
| Bia |
| (null) |
| (null) |
| (null) |
| (null) |
| (null) |
I made an example at Sqlfindle to better understand.
Question:
Theoretically, there is the difference, I would like to understand for which the difference, and the reason for having the two terms, of how the two were created, and created by someone.
Thank you.
Your own question has the answer. and both go back maria and Bia because it has in the two tables.
– Marco Souza
@GOKUSSJ4, right, the two tables have the values, but if the two tables, are different, would not bring anything. So far nothing more. What puzzles me is using
Right
andLeft
, and return the same thing, in the example given. Seriously, I can’t see practical differences for the two joins, and let’s disregard theOuter
.– David
In the answers of this question explains the differences of left and right.
– gato
Thank you @Denercarvalho, but specifically my question does not. Thank you anyway.
– David
What’s more stackoverflow is questions with Join ....
– Marco Souza
@David in the duplicate is well explained the difference between Left and Right and are not equal, are contrary.
– Jorge B.
Your question starts from an incorrect statement, impossible for both situations to return what you said. I put a few extra columns in your fiddle to make the difference more visible: http://sqlfiddle.com/#! 9/7ba72/1 - Compare the two diagrams I drew, which is easier. There is an example of left and right in the given question, and it is clear that they are not equal. Pay attention to what happens when you haven’t given on either side, and see the difference. When so, I suggest leaving a comment in the original post, it is more organized, and concentrates everything in one place.
– Bacco
Just to be clear: follow Exactly your fiddle, with both darlings, see the difference: http://sqlfiddle.com/#! 9/d56008/7 (pay particular attention to us null returned. This makes all the difference when you take several columns in a real situation).
– Bacco
A practical example: If you do LEFT JOIN users and phones, you will receive all users, even if they do not have a phone, which would be expected. If you do RIGHT in this case, you will not list users without a phone. And if you have a user-free phone, it will appear only with RIGHT (which should not even exist at first, but that’s another problem). LEFT is important when the table on the left is the "axis" of the information. An INNER wouldn’t return users without a phone either, and an OUTER would bring orphaned phones, which wouldn’t make sense if you’re looking for users' registration.
– Bacco