2

I know the first select from query is the left table, but I am still confused when to use right and left join:

ex:

select chann.name from investments inv
right join channels chann on chann.id = inv.channel_id;


select chann.name from channels chann
left join investments inv on chann.id = inv.channel_id;

They both return the same result, why in first query is used right and why left on second one, Does this have to do with Foreign Key something?

Thanks

Lulzim
  • 547
  • 4
  • 9
  • 22
  • have you tried anything ? http://stackoverflow.com/questions/3308122/how-do-i-decide-when-to-use-right-joins-left-joins-or-inner-joins-or-how-to-dete http://stackoverflow.com/questions/5706437/whats-the-difference-between-inner-join-left-join-right-join-and-full-join – Kaushik Apr 29 '15 at 13:54
  • 1
    A left join B is the same as B right join A. But humans usually find it much harder to understand right join. That's why most people use left join only. – jarlh Apr 29 '15 at 13:57
  • This may help you to understand the difference in SQL joins: http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins – Fred Apr 29 '15 at 13:58

2 Answers2

3

In your example both query are the same, because when change right to left you also change tables order. For simple understanding this, consider follow example:

SELECT A.* FROM Table1 A LEFT JOIN Table2 B on A.key=B.Key

In this case, LEFT JOIN means all records from Table1, because is the table in the left. If you use right means all records from table 2

ericpap
  • 2,917
  • 5
  • 33
  • 52
  • What does "in the left" mean? Do you mean to the left of the join statement "LEFT JOIN"? To what does the positionality right or left relate? – fraxture Jun 28 '19 at 21:01
  • @fraxtureWhen you use `FROM TableA`, that's your starting point located on the LEFT, any other joined table that comes later is on the RIGHT... – Enissay Jul 29 '19 at 10:19
1

LEFT JOIN: Return all rows from the left table, and the matched rows from the right table RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table

Meaning that a left join will take all the records from your first table and join them only with the records on the right table that adhere to your ON condition A right join will do the oposite (take all from second table and join with only records on first table that adhere to ON condition)

Jeremy C.
  • 2,405
  • 1
  • 12
  • 28