85

Wikipedia states:

"In practice, explicit right outer joins are rarely used, since they can always be replaced with left outer joins and provide no additional functionality."

Can anyone provide a situation where they have preferred to use the RIGHT notation, and why? I can't think of a reason to ever use it. To me, it wouldn't ever make things more clear.

Edit: I'm an Oracle veteran making the New Year's Resolution to wean myself from the (+) syntax. I want to do it right

4
  • 3
    Or, put another way, you want to do it right and left (and full) :)
    – Andriy M
    Commented Mar 16, 2016 at 12:36
  • can be, sure, but correctly be? comes down to how limited a developers understanding of right outer joins is. They are NOT the same or they wouldn't be there. #sigh
    – Dawesi
    Commented Apr 11, 2018 at 4:53
  • 1
    Same result can be achieved. As "A Left Join B" is equivalent to "B right join A", only difference is ordering of columns will get changed in output.
    – Deepak
    Commented Jan 23, 2020 at 18:33
  • 1
    example here stackoverflow.com/a/7313507/73226 Commented Apr 22, 2020 at 19:47

10 Answers 10

49

The only reason I can think of to use RIGHT OUTER JOIN is to try to make your SQL more self-documenting.

You might possibly want to use left joins for queries that have null rows in the dependent (many) side of one-to-many relationships and right joins on those queries that generate null rows in the independent side.

This can also occur in generated code or if a shop's coding requirements specify the order of declaration of tables in the FROM clause.

2
  • 3
    +1, would accept answer if it summarized yours, Michael, and Ivan's.
    – DCookie
    Commented Jan 15, 2009 at 18:57
  • 1
    we can able get same results using left outer join (by the way we get using right outer join).then why there is option for right join or vice versa?.
    – Hell Boy
    Commented Feb 22, 2016 at 7:32
32

I've never used right join before and never thought I could actually need it, and it seems a bit unnatural. But after I thought about it, it could be really useful in the situation, when you need to outer join one table with intersection of many tables, so you have tables like this:

enter image description here

And want to get result like this:

enter image description here

Or, in SQL (MS SQL Server):

declare @temp_a table (id int)
declare @temp_b table (id int)
declare @temp_c table (id int)
declare @temp_d table (id int)

insert into @temp_a
select 1 union all
select 2 union all
select 3 union all
select 4

insert into @temp_b
select 2 union all
select 3 union all
select 5

insert into @temp_c
select 1 union all
select 2 union all
select 4

insert into @temp_d
select id from @temp_a
union
select id from @temp_b
union
select id from @temp_c

select *
from @temp_a as a
    inner join @temp_b as b on b.id = a.id
    inner join @temp_c as c on c.id = a.id
    right outer join @temp_d as d on d.id = a.id

id          id          id          id
----------- ----------- ----------- -----------
NULL        NULL        NULL        1
2           2           2           2
NULL        NULL        NULL        3
NULL        NULL        NULL        4
NULL        NULL        NULL        5

So if you switch to the left join, results will not be the same.

select *
from @temp_d as d
    left outer join @temp_a as a on a.id = d.id
    left outer join @temp_b as b on b.id = d.id
    left outer join @temp_c as c on c.id = d.id

id          id          id          id
----------- ----------- ----------- -----------
1           1           NULL        1
2           2           2           2
3           3           3           NULL
4           4           NULL        4
5           NULL        5           NULL

The only way to do this without the right join is to use common table expression or subquery

select *
from @temp_d as d
    left outer join (
        select *
        from @temp_a as a
            inner join @temp_b as b on b.id = a.id
            inner join @temp_c as c on c.id = a.id
    ) as q on ...
6
  • 10
    You don't need to nest a SELECT, you can nest a join instead: FROM @temp_d AS d LEFT OUTER JOIN (temp_a AS a INNER JOIN @temp_b AS b ON b.id = a.id INNER JOIN @temp_c AS c ON c.id = a.id) ON a.id = d.id. (The brackets are unnecessary, they are added just for readability.) It's true, though, that there are people who are undecided as to which syntax they abhor more, nested joins or right outer joins, so it's possible that right joins might work less badly for them. :)
    – Andriy M
    Commented Mar 16, 2016 at 12:35
  • Nice, i never used nested joins syntax as well :) Commented Dec 10, 2016 at 14:22
  • Actually, the last query with d as the main table looks a lot more readable - "One table is outer joined with a bunch of tables which are inner joined among themselves."
    – Teddy
    Commented Aug 27, 2019 at 12:52
  • If a query starts with "select * from a" that table sounds like the main table. When some table much later in the sequence starts poking holes in the main table, it just seems weird.
    – Teddy
    Commented Aug 27, 2019 at 12:55
  • Also, which columns can be referred in the following join "on" parts? Only the "main" table, or any of the preceding tables? I've not given it so much thought.
    – Teddy
    Commented Aug 27, 2019 at 12:58
29

B RIGHT JOIN A is the same as A LEFT JOIN B

B RIGHT JOIN A reads: B ON RIGHT, THEN JOINS A. means the A is in left side of data set. just the same as A LEFT JOIN B

There are no performance that can be gained if you'll rearrange LEFT JOINs to RIGHT.

The only reasons I can think of why one would use RIGHT JOIN is if you are type of person that like to think from inside side out (select * from detail right join header). It's like others like little-endian, others like big-endian, others like top down design, others like bottom up design.

The other one is if you already have a humongous query where you want to add another table, when it's a pain in the neck to rearrange the query, so just plug the table to existing query using RIGHT JOIN.

3
  • +1, would accept answer if it summarized yours, Jekke, and Ivan's.
    – DCookie
    Commented Jan 15, 2009 at 18:56
  • no performance gained... not a great reason to learn to use the language as it was intended. If everything was done for performance reasons ruby and php and .net wouldn't be around anymore.
    – Dawesi
    Commented Apr 11, 2018 at 4:56
  • Yep, agree on the last paragraph. That might be a valid reason - but I'd wager that in this case, it is a rather bad practise that produces unmaintainable code... Commented Nov 2, 2021 at 9:11
9

The only time I would think of a right outer join is if I were fixing a full join, and it just so happened that I needed the result to contain all records from the table on the right. Even as lazy as I am, though, I would probably get so annoyed that I would rearrange it to use a left join.

This example from Wikipedia shows what I mean:

SELECT *  
FROM   employee 
   FULL OUTER JOIN department 
      ON employee.DepartmentID = department.DepartmentID

If you just replace the word FULL with RIGHT you have a new query, without having to swap the order of the ON clause.

2
  • How did the FULL statement get there in the first place? Badly written confusing query?
    – dkretz
    Commented Jan 12, 2009 at 23:39
  • 6
    FULL has it's purpose, and doesn't always need "fixing", and replacing it with RIGHT does not do the same thing. What if you're building a report to help the company validate employee-department assignments and want to include departments which have no employees as well as employees which have not been assigned to a department?
    – utexaspunk
    Commented Feb 12, 2014 at 15:51
5
SELECT * FROM table_a
INNER JOIN table_b ON ....
RIGHT JOIN table_c ON ....

How else could you quickly/easily inner join the first 2 tables and join with table_c while ensuring all rows in table_c are always selected?

2
  • 12
    You could list table_c first and left join it to the others.
    – DCookie
    Commented Oct 7, 2012 at 18:39
  • 2
    Writing it out, @Matt's syntax seems more readable than the equivalent: SELECT * FROM table_c LEFT JOIN ( SELECT * FROM table_a INNER JOIN table_b ON .... ) ON ....
    – 8forty
    Commented Jan 1, 2018 at 20:15
4

I've not really had to think much on the right join but I suppose that I have not in nearly 20 years of writing SQL queries, come across a sound justification for using one. I've certainly seen plenty of them I'd guess arising from where developers have used built-in query builders.

Whenever I've encountered one, I've rewritten the query to eliminate it - I've found they just require too much additional mental energy to learn or re-learn if you haven't visited the query for some time and it hasn't been uncommon for the intent of the query to become lost or return incorrect results - and it's usually this incorrectness that has led to requests for me to review why the queries weren't working.

In thinking about it, once you introduce a right-join, you now have what I'd consider competing branches of logic which need to meet in the middle. If additional requirements/conditions are introduced, both of these branches may be further extended and you now have more complexity you're having to juggle to ensure that one branch isn't giving rise to incorrect results.

Further, once you introduce a right join, other less-experienced developers that work on the query later may simply bolt on additional tables to the right-join portion of the query and in doing so, expanding competing logic flows that still need to meet in the middle; or in some cases I've seen, start nesting views because they don't want to touch the original logic, perhaps in part, this is because they may not understand the query or the business rules that were in place that drove the logic.

1
  • didn't read the comment above then? Isn't it weird how programmers think because they haven't used something that it is somehow redundant, when in fact perhaps they should update their skillset to understand edge cases.
    – Dawesi
    Commented Apr 11, 2018 at 4:51
3
SELECT * FROM table1 [BLANK] OUTER JOIN table2 ON table1.col = table2.col

Replace [BLANK] with:

LEFT - if you want all records from table1 even if they don't have a col that matches table2's (also included are table2 records with matches)

RIGHT - if you want all records from table2 even if they don't have a col that matches table1's (also included are table1 records with matches)

FULL - if you want all records from table1 and from table2

What is everyone talking about? They're the same? I don't think so.

7
  • 3
    You can always replace a RIGHT OUTER JOIN with a LEFT OUTER JOIN that produces the same answer, and vice-versa.
    – DCookie
    Commented Jan 12, 2009 at 18:50
  • Do you mean you just swap out the words "LEFT" and "RIGHT" or do you mean you flop around the syntax of the query? Commented Jan 12, 2009 at 19:54
  • 4
    you have to change the order of the tables inthe join as well.
    – HLGEM
    Commented Jan 12, 2009 at 21:18
  • @Andrew: the latter. One can always express a given RIGHT OUTER JOIN with an equivalent LEFT OUTER JOIN, e.g., select ... from a right outer join b using(x); can be expressed as select ... from b left outer join a using(x); Hence the question, why use a RIGHT OUTER JOIN?
    – DCookie
    Commented Jan 12, 2009 at 21:29
  • 1
    I agree that RIGHT OUTER JOINs are very rarely used but saying they are the same is very misleading to someone who is new to SQL and JOINs. I think the only time I've used a ROJ is on an MS Access "query" -- what a nightmare :/ Commented Jan 13, 2009 at 9:59
2

SQL statements, in addition to being correct, should be as easy to read and expressively concise as possible (because they represent single atomic actions, and your mind needs to grok them completely to avoid unintended consequences.) Sometimes an expression is more clearly stated with a right outer join.

But one can always be transformed into the other, and the optimizer will do as well with one as the other.

For quite a while, at least one of the major rdbms products only supported LEFT OUTER JOIN. (I believe it was MySQL.)

2
  • 1
    "Sometimes an expression is more clearly stated with a right outer join." I'm struggling with when that might be the case. Why use a syntax that is basically the inverse of another, more commonly used one? Do you have an example?
    – DCookie
    Commented Jan 12, 2009 at 22:59
  • 1
    Nope. I've never used RIGHT OUTER JOIN myself :D.
    – dkretz
    Commented Jan 12, 2009 at 23:37
2

The only times I've used a right join have been when I want to look at two sets of data and I already have the joins in a specific order for the left or inner join from a previously written query. In this case, say you want to see as one set of data the records not included in table a but in table b and in a another set the records not in table b but in table a. Even then I tend only to do this to save time doing research but would change it if it was code that would be run more than once.

2

In some SQL databases, there are optimizer hints that tell the optimizer to join the tables in the order in which they appear in the FROM clause - e.g. /*+ORDERED */ in Oracle. In some simple implementations, this might even be the only execution plan available.

In such cases order of tables in the FROM clause matters so RIGHT JOIN could be useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.