r/SQL • • Mar 25 '26

Discussion When to use RIGHT JOIN instead of switching tables in Left Join

Hello all! I need some help with understanding the utility of Right join. I have 2 tables: Orders and Customers. If I write Orders RIGHT JOIN Customers it returns the same results as switching tables and using left join, like Customers LEFT JOIN Orders. But when these 2 examples can give different results? Like when it is mandatory to use right join instead of just switching tables in left join?

35 Upvotes

85 comments sorted by

View all comments

Show parent comment

1

u/tehfarmer Mar 25 '26

i'm trying to come up with a scenario where a right join would be preferable and i can't think of one, though you've piqued my curiosity. do you have an example of what you're talking about here?

2

u/jshine13371 Mar 25 '26

I have an example, which is probably additional to u/ComicOzzy's:

If you're using MySQL and need to emulate a FULL JOIN. MySQL doesn't offer that as a dedicated keyword. Instead the normal solution is to do a LEFT JOIN and UNION the results to a RIGHT JOIN between the same tables. That is logically equivalent to a FULL JOIN.

Yes, you can UNION two LEFT JOINs instead by re-arranging the tables in the second half of the UNION, but the readability is better as a RIGHT JOIN to more clearly establish intent with this pattern in MySQL.

2

u/tehfarmer Mar 25 '26

Interesting! I have never used MySQL so didn't realize a full outer join didn't exist there. I learned something today!

Are there any examples in T-SQL where a right join is explicitly required?

2

u/jshine13371 Mar 25 '26

I don't think there's any examples ever where RIGHT JOIN is explicitly required, as like most things, there's always an alternative. Just use cases where it's preferred.

But the same example I mentioned above also applies to SQL Server / T-SQL for a different reason. FULL JOIN tends to be less performant of an implementation than using the UNION implementation I described, particularly when additional transformations are applied on top of it. Not always the case, but frequent enough that it's good to be aware of the UNION technique as an optimization strategy.

1

u/Wise-Jury-4037 :orly: Mar 25 '26

FULL JOIN tends to be less performant of an implementation than using the UNION implementation

Why/when? Do you have some examples of queries/query plans to demonstrate this?

1

u/jshine13371 Mar 26 '26

One example is when you're filtering the fully joined dataset that requires an OR operator in a predicate against the FULL JOIN implementation but can be rewritten as a separate predicate, for each of the UNIONed datasets, therefore improving sargability.

There's more complex use cases I've encountered where generally re-writing as the UNION implementation was more performant, but I don't recall the exact scenarios offhand.

(Btw the UNION implementation can be a UNION ALL depending on the use case / context too.)

0

u/Wise-Jury-4037 :orly: Mar 26 '26

This is not specifically FULL JOIN issue - your general statement "FULL JOIN tends to be less performant of an implementation" is false therefore.

Outer joins also sometimes are better rewritten as unions of some kind if there are "OR" conditions.

0

u/jshine13371 Mar 26 '26 edited Mar 26 '26

You're saying a statement I made that is non-absolute ("tends" - my words, followed up with "Not always the case") is absolutely wrong ("is false therefore") because it's a subset of a larger group of things that are also true ("Outer join's also sometimes are better rewritten as unions of some kind... - your words). You realize how silly what you just said is?

Not to mention there are other use cases, exclusive to FULL JOINs where my previous statement holds true, yet you decided to definitively rule it as "false" anyway. I guess if you knew everything already, your reply asking for more information was pointless...

I'm not going to argue with someone who's looking for an argument and is lacking reading comprehension.

0

u/Wise-Jury-4037 :orly: Mar 26 '26 edited Mar 26 '26

I'm not going to argue with someone who's looking for an argument and is lacking reading comprehension.

You shouldnt argue then, otherwise someone can tell that you have duplicitous intent?

lacking reading comprehension

Super low effort at ad hominem. You're terrible at arguing, objectively.

You're saying a statement I made that is non-absolute ("tends" - my words, followed up with "Not always the case") is absolutely wrong ("is false therefore") because it's a subset of a larger group of things that are also true ("Outer join's also sometimes are better rewritten as unions of some kind... - your words). You realize how silly what you just said is?

Semantics argument is, again, super low effort, especially if you get it wrong. And you are wrong in this case, despite hiding behind vagueness of your statements.

In your statement you've singled out FOJ as "tends to be less performant of an implementation than using the UNION implementation I described", That a general statement ("tends" would imply over 50%, at the very least). I've asked you to support it by examples and you've brought up an example that is NOT specific to FOJ at all (it could have been defensible if you said "joins with OR conditions sometimes can benefit from being re-written as unions", but you said no such thing).

EDIT/PS: If you dont understand how extra descriptors change the scope and focus of your statement, consider the difference between these 2 statements:

Child molesters are usually manipulative

Black child molesters are usually manipulative

Anyways, your suggested approach is pretty bad for rewrites and you arent providing any valuable technical counterpoints, I'm not even remotely interested in your ad homimens.

Comprehend this?

1

u/jshine13371 Mar 26 '26

I'm not going to argue with someone who's looking for an argument and is lacking reading comprehension

😘 

1

u/Wise-Jury-4037 :orly: Mar 25 '26

never needed a full join in MySQL, but why would you choose UNION + Right JOIN vs UNION ALL/Left JOIN + NOT EXISTS? Are sorts that much better than subqueries in MySQL?

1

u/jshine13371 Mar 25 '26

never needed a full join in MySQL

The use cases for a FULL JOIN are platform independent.

but why would you choose UNION + Right JOIN vs UNION ALL/Left JOIN + NOT EXISTS?

Your question is comparing apples and oranges, making it difficult to answer. UNION and UNION ALL affect the results differently.

I'm also not sure what you mean by using a NOT EXISTS, you can provide an example if you want to clarify. But it sounds like an extra operation so probably would be theoretically less efficient.

1

u/Wise-Jury-4037 :orly: Mar 25 '26 edited Mar 25 '26

Your question is comparing apples and oranges, making it difficult to answer. UNION and UNION ALL affect the results differently.
I'm also not sure what you mean by using a NOT EXISTS, you can provide an example if you want to clarify

/facepalm it's only "apples and oranges" if you cannot replicate functionality

So, instead of the "standard"

select a.*, b.*

from a full outer join b on a.some_id = b.some_id

your "typical mysql" approach (which is deceptively simple but most likely requires 2 sorts and a distinct/merge):

select a.*, b.*

from a left join b on a.some_id = b.some_id

union

select a.*, b.*

from a right join b on a.some_id = b.some_id

the alternative would be something like this:

select a.*, b.*

from a left join b on a.some_id = b.some_id

union all

select a.*, b.*

from b

left join (select * from a where 1=2) a on 1=2

where not exists ( select * from a where a.some_id = b.some_id)

1

u/jshine13371 Mar 26 '26

That's some very funky code you wrote there mate proving my point about readability.

0

u/Wise-Jury-4037 :orly: Mar 26 '26

Proving how? By re-writing what might be generally bad code and advice?

1

u/jshine13371 Mar 26 '26

Proving how?

Again, come back to me after you've improved your reading comprehension, before you want to have an argument mate. Cheers!

1

u/ComicOzzy sqlHippo Mar 26 '26

I'm reposting this from another comment I left in a previous RIGHT JOIN discussion:

OK, so you have a query where you want to take table A and LEFT JOIN it to the result of an INNER JOIN between tables B and C. You can do that in a few different ways.

One is to make a verbose subquery or CTE:

FROM A 
LEFT JOIN ( SELECT ... 
            FROM B 
            INNER JOIN C 
            ON B.pk = C.fk
          ) S 
ON A.pk = S.fk  

Another is to use a confusing nested join:

FROM A
LEFT JOIN B 
INNER JOIN C 
ON B.pk = C.fk
ON A.pk = B.fk

Or... you could write a simple, perfectly normal RIGHT JOIN:

FROM B 
INNER JOIN C
ON B.pk = C.fk
RIGHT JOIN A
ON A.pk = B.fk

Now, I'd like to add that I know this isn't going to convince anyone to start using RIGHT JOIN in their production code. I fully expect they'd choose the subquery/CTE method instead. I just like to push back against the blind rage against RIGHT JOIN that scares away other people from bothering to learn it, much the same way kids learn to fear math if their parents are also easily frustrated by it.