Difference between RIGHT JOIN and LEFT JOIN

Asked

Viewed 12,161 times

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).

Left Join

RIGHT JOIN
Return all records from the right table (table B) and any matches with the left table (table A).

Right Join

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.

  • @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 and Left, and return the same thing, in the example given. Seriously, I can’t see practical differences for the two joins, and let’s disregard the Outer.

  • In the answers of this question explains the differences of left and right.

  • Thank you @Denercarvalho, but specifically my question does not. Thank you anyway.

  • What’s more stackoverflow is questions with Join ....

  • 1

    @David in the duplicate is well explained the difference between Left and Right and are not equal, are contrary.

  • 2

    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.

  • 2

    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).

  • 3

    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.

Show 4 more comments

2 answers

5


Own experience:

Depending on how you diagram your bd, may have n tables, and depending on the need of your query, you may need to use a table as the main of your query, exemplifying with a diagramming without much embellishment:

inserir a descrição da imagem aqui

In the example I have 6 tables, a tabela 3 clearly is the main of my system, it which makes connection with all the others, could be a table of plans, for example...

If by chance I want to create just one query bringing information concerning the id of tabela 3, and other additional information from other tables, for example I may want to bring all information regarding tabela 4, but which has no connection with the tabela 2 and 6, then I could use both the resources of the left join, how much of right join so that both could meet my need.

In mathematical terms, remembering that little class of sets, it might be easier to visualize:

inserir a descrição da imagem aqui

In this example, I need to get information that is primarily on tabela 1 and not in the tabela 2, and are primarily in the tabela 5 but not in table 6.

There are two ways of doing such a query, one using sub-querys (The most recommended when there are conditions in the query of the "sub-domain", which do not depend only on the relationship of the tables), or other that is using the resource of the left join and right join to bring only the data that is needed from one or another table, in which case the select would look something like this [edit]

  • I understood a little, if I could create an example it would be incredible.

  • 1

    I remember a query that I worked once, which was student referral, I had to work with about 10 tables in a query only hsuashaushas, and it was full of inners, left, and rigth joins at the same time to bring no result null, create an example without a basis is complicated. If I can’t do it today, then I look for the code and put it here.

3

In terms of functioning there is no difference only in terms of concept as the name says righ or left or left if you use

SELECT *
  FROM tabela_a a
  LEFT JOIN tabela_b b
  ON b.nome = a.nome;

you will bring all the results of the table that is left this tabela_a EVEN those that do not match the table on the right. In the case if there is no comparison it brings null.

If you trade it for the right it would bring all the results of tabela_b (to the right of table A).

  • If in terms of operation, there is no difference, why did you create the two terms? This is intriguing me.... thank you

  • 2

    simple imagine a query where you have several tables colliding or not information, it is easier to tell which side of the query will return everything he finds than to change the whole query by moving the table names to suit it. Just changing from left to right already makes a huge difference that is very useful when you gather or go get information in many tables at once.

  • I understand, and it really makes a huge difference, but then I would use the OUTER JOIN, in case the IS NULL at the end of JOIN.

  • Outer Join he SOMETHING else face as your drawing showed all this is based on set theory read on here I hope you help http://answall.com/questions/6441/qual%C3%A9-a-difference%C3%A7a-entre-Inner-Join-e-outer-Join

  • I quoted this answer at the beginning of my question. Still, thank you.

  • sorry rsss, then Outher Join or full Join brings EVERYTHING I had in tebala a e b even if there is no existing data that connect the 2, summarizing it makes a mess and this almost never has logical application. right and left it brings the data from one table but not from the other if there is no collidence.

  • 3

    @David is really just for the sake of being practical, you could also make yours ifonly with > without the < right? then why have both?

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.