1

Can we use right join instead of left join and vice versa by just flipping the table position in the query.If so then why we need both joins?

mehdi lotfi
  • 11,194
  • 18
  • 82
  • 128
Nikhil
  • 21
  • 1

3 Answers3

1

Yes. Following query have same result:

SELECT *
FROM table1
LEFT JOIN table2 ON table1.column1 = table2.column2


SELECT *
FROM table2
RIGHT JOIN table1 ON table1.column1 = table2.column2
mehdi lotfi
  • 11,194
  • 18
  • 82
  • 128
0

If a developer wants all records from #t1 and only matching records from #t2, then he can write the query as Select * from #t1 left join #t2 or Select * from #t2 right join #t1

Both are same. They give same results.

First, there are more than just these 2 types of joins. There is also a FULL OUTER JOIN, in which case non-matching rows from both sides are included in the result. Additionally, an INNER JOIN (matching rows only) and CROSS JOIN (cartesian product) are available.

In a simple query like the examples in this thread, one can use LEFT or RIGHT and reverse the tables to return the desired results. But as additional tables are added to the query, it may be easier to add those with different join type rather than refactoring the entire query to achive the same semantics.

Jay Nirgudkar
  • 426
  • 4
  • 18
0

Yes, the only difference is the order in which you write the joined tables, you can rewrite LEFT into RIGHT and vice versa. So strictly speaking, we do not need to have both joins.

There is a discussion on why people use RIGHT joins.

Community
  • 1
  • 1
Thilo
  • 257,207
  • 101
  • 511
  • 656