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

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

😘