6

For some time I have only been knowing how to use the INNER JOIN and absolutely no clue about what LEFT/RIGHT (OUTER) JOIN does. Although, as I just read about it, I cannot see what purpose the RIGHT has?

It seems to me it's identical to a reverse LEFT JOIN

If we follow my example:

SELECT t1.* FROM table1 t1 RIGHT JOIN table2 t2 ON t2.value = t1.value

Would be identical to:

SELECT t2.* FROM table2 t2 LEFT JOIN table1 t1 ON t1.value = t2.value

Is this right, or am I missing something out?

Colandus
  • 1,634
  • 13
  • 21
  • http://stackoverflow.com/a/248403/40822 – dotjoe Feb 27 '13 at 19:46
  • Yea I did some bad searching before posting, my bad! Clearly not only one duplicate... – Colandus Feb 27 '13 at 19:54
  • It's possible that the ANSI committee on SQL created both RIGHT and LEFT OUTER JOIN so that SQL programmers could engage in endless advocacy debates about some language feature other than the validity of NULL! :-) – Bill Karwin Feb 27 '13 at 19:58

3 Answers3

3

Yes, you're right. From Wikipedia:

A right outer join (or right join) closely resembles a left outer join, except with the treatment of the tables reversed. Every row from the "right" table (B) will appear in the joined table at least once. If no matching row from the "left" table (A) exists, NULL will appear in columns from A for those records that have no match in B. A right outer join returns all the values from the right table and matched values from the left table (NULL in case of no matching join predicate).

j08691
  • 204,283
  • 31
  • 260
  • 272
1

Yes this is right, you can find more information if you searched:

Difference between left join and right join in SQL Server

Community
  • 1
  • 1
mlemay
  • 1,622
  • 2
  • 32
  • 53
1

Right join is equivalent to a reverse left join.

That doesn't make it useless. Some people may prefer to write their queries that way. I am not one of those people. Personally, I almost never use right outer joins. And, no query should have both left and right outer joins.

There might be cases where someone would prefer:

select . . .
from table t right outer join
      (very
       long
       subquery
       here
      ) t2
      on . . .

To putting the table second.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786