15.7 Join Performance: How the Planner Chooses Join Strategies

Right, so you’ve written your beautiful JOIN clause. You feel good. You’re a SQL-wielding hero. But have you ever stopped to wonder what unholy incantations the database mutters under its breath to actually execute that thing? It doesn’t just magically combine data. It has a whole bag of tricks, officially called join strategies, and the planner picks one based on a cold, hard calculation of expected cost. Let’s pull back the curtain.

15.6 LATERAL: Correlated Subquery-Style Joins

Alright, let’s talk about LATERAL. This is the one that makes people feel like they’ve leveled up. It’s the JOIN that lets you break the most fundamental rule of SQL you’ve probably internalized: that everything in your FROM clause happens in its own little isolated bubble. Think of a standard subquery in your SELECT list. It can’t see the individual row from the main table it’s being compared to until after the main FROM clause has done its thing. It’s like shouting questions to a friend in another room. A LATERAL join, on the other hand, is like turning to the person right next to you and asking a question for each row. It’s a correlated subquery on steroids, and it’s executed row-by-row alongside the table it’s joining to.

15.5 Self-Joins: Hierarchical and Comparison Queries

Alright, let’s talk about self-joins. Don’t let the name intimidate you; it’s one of the most conceptually simple yet powerful tools in the SQL toolbox. The “self” part just means you’re joining a table to itself. You’re not summoning a demon or creating a time paradox. You’re essentially treating the single table as two separate logical entities for the duration of the query. Why would you do this? Primarily for two brilliant reasons: to untangle hierarchical data and to perform row-by-row comparisons.

15.4 CROSS JOIN: The Cartesian Product

Alright, let’s talk about the CROSS JOIN. This is the one that feels less like a useful tool and more like you accidentally leaned on the keyboard and created a database monster. It’s the SQL equivalent of saying, “What if… everyone dated everyone?” The results are predictably chaotic and almost always enormous. In a perfectly dry, technical sense, a CROSS JOIN returns the Cartesian product of the two tables involved. Fancy term. All it means is that every single row from the first table is combined with every single row from the second table. There is no “on” condition. There is no logic. There is only multiplication. If Table A has 10 rows and Table B has 20 rows, your result set will be 200 rows. If Table A has 1,000 rows and Table B has 5,000 rows, you get 5,000,000 rows. You see where this is going. This operation has exponential growth written all over it, and it’s the easiest way to get your database to look at you like you’ve just asked it to solve a thermodynamics equation in a hurricane.

15.3 RIGHT JOIN and FULL OUTER JOIN

Now, let’s talk about the two JOINs that everyone seems to find a little… odd. The RIGHT JOIN and FULL OUTER JOIN are often treated like the weird cousins at the family reunion—you know they’re important, but you’re not quite sure how to talk to them. Let’s fix that. The truth is, a RIGHT JOIN is just a LEFT JOIN in a cheap mirror. It’s functionally identical. No, really. A RIGHT JOIN on tables A and B is literally just a LEFT JOIN on tables B and A. The only difference is the order in which you write the tables in your query. Because of this, you will almost never see a RIGHT JOIN in professional, production-grade code. It’s the syntactic equivalent of wearing your pants backwards. It works, but it confuses the hell out of everyone who sees it, including Future You at 2 AM trying to fix a bug.

15.2 LEFT JOIN: All Rows from the Left, NULLs Where No Match

Right, let’s talk about the LEFT JOIN. This is the workhorse of the relational world, the one you’ll use more often than any other. It’s the Swiss Army knife for when you want to ask, “Show me everything from this table, and if you happen to have any matching info from that table, tack it on. If you don’t, just give me NULLs and I’ll deal with it.” The mental model is simple, but the implications are everything. The official definition is: a LEFT JOIN returns all records from the left table (the one you mention first, before the JOIN keyword), and the matched records from the right table (the one after JOIN). The result is NULL from the right side if there is no match.

15.1 INNER JOIN: Matching Rows in Both Tables

Right, let’s get into the workhorse of the SQL world: the INNER JOIN. Forget the fancy stuff for a moment; this is the join you’ll use 80% of the time. The concept is beautifully simple: it returns only the rows where there’s a match in both tables you’re joining. It’s the set intersection of your data. If a row in the left table doesn’t have a corresponding partner in the right table, it gets left on the cutting room floor. The same goes for a row in the right table with no match on the left. It’s a mutual agreement for data entry.

— joke —

...