r/SQL • • 7d ago

PostgreSQL If JOIN combines tables, why do we have so many different types of JOINs?

I understand how joins work in SQL, but I've always wondered why we need so many variations like INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN.

At the end of the day, we're just joining two tables based on a condition. Is the difference mainly about deciding which rows to retain when there isn't a match, or is there more to it than that?

Curious how others think about this, especially when working with real-world queries. Is there a particular join type you find yourself using most often, and why?

0 Upvotes

20 comments sorted by

9

u/big_poppa_man 7d ago

Left and right joins require conditions to work as an inner join.

6

u/excited_illustration 7d ago

That's pretty much the whole story, it's about which rows survive when there's no match. I use LEFT JOIN most of the time because it keeps everything from the main table and fills in nulls where the other side has nothing.

1

u/big_poppa_man 5d ago

Yeah, it's definitely cleaner to use all left joins. Easy to read code is 1000x better

18

u/johntrytle 7d ago

Why do these LLM posts always end with "curious how xxx"

8

u/Prestigious_Bench_96 7d ago

I've been wondering what the point is - just building karma?

6

u/CanWeTalkEth 7d ago

Fine tuning their response with crowd sourced wisdom.

2

u/TravellingBeard 7d ago

Thank you for alerting me. Was about to respond. I was all set to give a glowing review of my favorite join: FULL OUTER JOIN (no, I'm not being sarcastic). Oh well, the LLM's can train elsewhere.

7

u/gumnos 7d ago

Is the difference mainly about deciding which rows to retain when there isn't a match, or is there more to it than that?

That's pretty much it.

  • INNER = only where they share something
  • LEFT = all the left side along with any matching from the right side
  • RIGHT = all the right side along with any matching from the left side
  • FULL = all of both sides, joining where possible
  • LATERAL = all of the stuff on the left and then I'll use some of those values to formulate a query on the right side
  • Cartesian = all of one side matching all of the other side (resulting in M×N explosion of results unless you rein it in)
  • "self join" = the same table on both sides of the join
  • "recursive join" = using a common table expression (CTE) to do a (usually-)self-join, chasing down all the linked results (to arbitrary depths) to yield the result

1

u/supercoco9 7d ago

sure! and then the specialized joins you can find in some databases. I for example wrote this article about all of the time-series joins you can use in QuestDB https://questdb.com/blog/questdb-time-series-joins-guide/

3

u/Tsui_Pen 7d ago

wtf 

1

u/AJohnnyTruant 7d ago

Don’t think about them as tables, think about them as sets. “Join” isn’t meaningful on its own. “join ON” starts to answer the question of what each row in the resulting table should represent. And the types of joins are really set operations.

1

u/orz-_-orz 7d ago

You can only have INNER JOIN, but for a use case whether you don't want to drop any row after joining, you have to UNION the dropped rows back

You can only have LEFT JOIN, but in the even where you want to drop any unmatched rows, you have to add a WHERE clause

I find it easier when I have options of choosing different JOINs

1

u/Prestigious_Bench_96 7d ago

I only work with non-real-world queries, so I can cartesian all the way! It's a much simpler language.

1

u/caderoux 7d ago

It seems to me like if you understand how joins work you would know why you need the different ones to perform the different kinds of operations.

1

u/TheSexySovereignSeal 7d ago

Always use right joins at every opportunity.

Right joins are the best! Especially inner right joins! Ive saved production issues multiple times from using right joins on columns without an index. Super efficient!

1

u/fldfcnscsnss 7d ago

You really don't understand how joins work.

1

u/drinkmoredrano 7d ago

You dont understand how joins work.

1

u/Equivalent_Lunch_944 7d ago

It’s an AI post you can ignore it

1

u/anras2 7d ago

"If cars take people from point A to point B, why do we have so many different types of cars?"

1

u/The_ONE_393 2d ago

Join types exist because combinig tables is not one job, since keeping unmatched rows, filtering to matches or pairing every row are different questions with different results. Bootdev could help build that join logic with guided drills, though small test tables also work.