Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is better.. left outer join or right outer join?

Tags:

sql

join

We can get the same result in both these ways..

Table_1 LEFT OUTER JOIN Table_2

Table_2 RIGHT OUTER JOIN Table_1

If we can get the same result why to use right outer join ? Which one is better ?

like image 860
Vaibhav Avatar asked Sep 09 '11 06:09

Vaibhav


People also ask

Which is better left join or right join?

The most substantial difference between the left and right outer join lies in the unmatched records that are obtained besides matched records. The left join takes all matching records and unmatched records of the left table while the right join takes all matching records and unmatched records of the right table.

Which is faster left outer join or right outer join?

A LEFT JOIN is absolutely not faster than an INNER JOIN . In fact, it's slower; by definition, an outer join ( LEFT JOIN or RIGHT JOIN ) has to do all the work of an INNER JOIN plus the extra work of null-extending the results.

When to use left outer join vs right outer join?

The main difference between the Left Join and Right Join lies in the inclusion of non-matched rows. Left outer join includes the unmatched rows from the table which is on the left of the join clause whereas a Right outer join includes the unmatched rows from the table which is on the right of the join clause.

Which join is more efficient?

TLDR: The most efficient join is also the simplest join, 'Relational Algebra'. If you wish to find out more on all the methods of joins, read further. Relational algebra is the most common way of writing a query and also the most natural way to do so.


2 Answers

As others have pointed out already LEFT OUTER JOIN and RIGHT OUTER JOIN are exactly the same operation, except with their arguments reversed. Your question is like asking whether it is better to write a < b or b > a. They're the same - it's just a matter of preference.

Having said that, I find that most people are more familiar with LEFT JOIN and use it consistently in their SQL. Some people even find it quite difficult to read if there is suddenly a RIGHT JOIN in the middle of a query, and it causes them to have to stop and think what it means. So I'd suggest that given an equal choice between the two options, prefer to use LEFT JOIN. Being consistent will make it easier for other developers to understand your SQL.

like image 132
Mark Byers Avatar answered Sep 21 '22 00:09

Mark Byers


Those are equal, just a matter of preference and readability. I assume its the same tables?

like image 37
rickythefox Avatar answered Sep 18 '22 00:09

rickythefox