I was talking to a friend of mine and they are learning some SQL and they said something that I have seen come up multiple times in learning SQL.
They said “Yeah, I need to study the join types more. They make sense to me but I want to be able to not reference my notes” and also “I don’t really get the point of a right join if your can do the same thing with a left join by just switching the table name.”
These are great points, and common questions that occur when first learning SQL.
Before I talk about LEFT/RIGHT JOINs, I wanted to mention that in my experience, idiomatic SQL does not contain RIGHT and OUTER joins, ignore them.
That might be contentious among folks, but unless you have a large amount of code written in a specific way where a RIGHT or OUTER join is the only thing that makes sense, fine, you’ve found the way, but generally you probably just want a subquery or CTE to encapsulate your logic and maintain “one way” of thinking about the problem.
The second piece of the puzzle that helped me a lot was understanding that all JOINS are just specialized versions if CROSS JOINs that help communicate your intent.
If we start with the following example, we’re making sure that every row in left and every row in right will touch. A classic CROSS JOIN.
SELECT *
FROM left AS l
CROSS JOIN right AS r
Now, a Cartesian product is rarely useful (unless you are making a numbers table!) but its the basis for the next step, a predicate (or WHERE clause) which allows us to restrict to only useful rows on the left hand side of the query.
SELECT *
FROM left AS l
CROSS JOIN right AS r
WHERE
l.id = r.left_id AND
r.left_id IS NOT NULL
Some knowledgeable folks in the audience might be confused why I would filter on r.left_id IS NOT NULL (given the original condition) but some SQL dialects match on NULLs!
This CROSS JOIN is equivalent to a LEFT JOIN that looks like
SELECT *
FROM left AS l
LEFT JOIN right AS r ON
l.id = r.left_id
The same applies to a RIGHT JOIN – just swap the main characters.
The INNER JOIN is even easier, it just makes sure there’s no unmatching values on both sides of the equation.
Now, this isn’t hugely different, but being able to call out LEFT LEFT LEFT LEFT INNER LEFT when you are reading through a long procedure is a critical part of reading SQL, swapping between RIGHT LEFT INNER all the time will absolutely make a difference in the mental model of your reviewer, and expect them to call you out if you use a RIGHT JOIN for anything but fun.
I hope you learned something from one of my early SQL insights, hopefully it can help you understand why there are multiple join types.