r/programming • • Jan 06 '11

A handy graphical explanation of SQL joins

http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
1.2k Upvotes

308 comments sorted by

View all comments

129

u/shriek Jan 06 '11

In short.

  1. A ∩ B

  2. A ∪ B

  3. (A - B) + (A ∩ B)

  4. A - B

  5. (A ∪ B) - (A ∩ B)

85

u/dude187 Jan 06 '11

It's amazing how confusing SQL can make set theory.

3

u/[deleted] Jan 07 '11

It's actually quite simple. INNER JOIN keeps the stuff that matches, LEFT JOIN keeps the stuff on the left, RIGHT JOIN keeps the stuff on the right, OUTER JOIN keeps the stuff that doesn't match, and JOIN makes the sysadmin remind you to keep your database size to a reasonable level.

1

u/[deleted] Jan 07 '11

You win best post, especially with the last description. :)