r/SQL • u/shdw_0x0 • 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
9
u/big_poppa_man 7d ago
Left and right joins require conditions to work as an inner join.
18
u/johntrytle 7d ago
Why do these LLM posts always end with "curious how xxx"
7
u/gumnos 7d ago
That's pretty much it.
INNER= only where they share somethingLEFT= all the left side along with any matching from the right sideRIGHT= all the right side along with any matching from the left sideFULL= all of both sides, joining where possibleLATERAL= all of the stuff on the left and then I'll use some of those values to formulate a query on the right side3
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.