NOTE: I highly recommend people read the Coding Horror extension of my post. I wrote this up as a quick and dirty cheat for a friend. Coding Horror actually takes his time with the subject and gives it a better explanation.
I was asked to post this after explaining it to someone on IRC.
If you have tried to understand how joins work and constantly get confused about what join to use, you just need to keep a simple picture in mind ( I like pictures). I will be explaining joins by referencing a Venn diagram. Have no fear – I won’t get into any of the set theory/math involved. This is just a basic overview to give you an idea the data a particular join will return to you. This is not a technical discussion – just concepts.
We will start with just an empty diagram:
The T1 circle represents all the records in table 1. The T2 circle represents all the records in table 2. Notice how there is a bit of overlap of the 2 circles in the middle. Simple right?
I will use red to signify the records that will be returned by a particular join.
An inner join only returns those records that have “matches” in both tables. So for every record returned in T1 – you will also get the record linked by the foreign key in T2. In programming logic – think in terms of AND.
An outer join is the inverse of the inner join. It only returns those records not in T1 and T2. “Give me the records that DON’T have a match.” In programming logic – think in terms of NOT AND.
A left join returns all the records in the “left” table (T1) whether they have a match in the right table or not.
If, however, they do have a match in the right table – give me the “matching” data from the right table as well. If not – fill in the holes with null.
It should be noted that the same thing is possible with a right join – most people just use a left one.
LEFT OUTER JOIN
A left outer join combines the ideas behind a left join and an outer join. Basically – if you use a left outer join you will get the records in the left table that DO NOT have a match in the right table.
Again it is noted that the same thing is possible with a right outer join – most people just use a left one.
A theta join is the Cartesian product of the 2 tables and not normally what people are looking for – but what they sometimes get by mistake. How many of us have written a join similar to this only to get way more then we were ever expecting.
SELECT t1.*, t2.*
FROM table1 t1, table2 t2
WHERE t1.id = 5;
So there you have the basic concepts of joins. Next time you need to use a join and have no clue what to do to get the data you need from the database, draw a picture. It may help you figure out what join to use. Least it helps me.
Important:Please be sure to read the comments below. There are definite situations where my simplistic drawings are invalid and brought to light.
Disclaimer: I am by no means a database guru. Please be sure to learn more about joins from the many online articles available.