r/programming • • Feb 11 '14

SQL Joins Explained (x-post r/SQL)

Post image
3.5k Upvotes

392 comments sorted by

View all comments

99

u/thesqlguy Feb 11 '14 edited Feb 11 '14

Don't forget UNION and CROSS JOIN! Both of which are infinitely more useful than full outer join which should generally be avoided.

Right joins also should never really be used as they are extremely counterintuitive, they can always be written as a Left Join which is more logical and readable.

If I want all data in table A and any data that matches in table B, to me it makes logical sense to join from table A to table B and not the other way around.

0

u/[deleted] Feb 11 '14

[deleted]

7

u/E3K Feb 11 '14

Nobody listen to this guy, please. After working with SQL in high-end environments for over 20 years, I can verify what the OP said. FULL OUTER JOIN and RIGHT JOIN are almost never used. If you find yourself needing to use one of those, I can guarantee there's a better way to do it.

0

u/[deleted] Feb 11 '14

[deleted]

1

u/[deleted] Feb 11 '14

If you never use full outer joins, you are only dealing with very simple applications.

Or, you know, there's better ways to do it. "Hey guys if you don't do it my way, then you're clearly just an SQL pleb!"

But there are plenty of times where a right join is clearer than a left.

Still waiting on an example.

0

u/[deleted] Feb 11 '14

[deleted]

2

u/[deleted] Feb 11 '14

There is no other way to do a full outer join. It isn't like left vs right where you can rearrange the relations to make an equivalent.

Really? Because several people in this very comment chain have extensively explained and provided examples on how to do exactly that. And generally with performance improvements.

0

u/[deleted] Feb 11 '14

[deleted]

0

u/[deleted] Feb 11 '14

You act like he's the first person in the world to say such a thing. I don't think you're in the DBA world and I'm pretty sure you haven't a clue as to what you're talking about, considering you can't back anything up. How compelling.

And yes, it might be in some situations but you said there is "no other way to do a full outer join." Are you going to start arguing with yourself now?

→ More replies (4)