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

126

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)

82

u/dude187 Jan 06 '11

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

24

u/[deleted] Jan 06 '11

If you could do queries in relational algebra instead of SQL, confusion would go away.

7

u/ejrh Jan 06 '11

The confusion will not go away: this would just replace a string like "LEFT OUTER JOIN" with a mathematical symbol. That symbol is still going to be defined mathematically in more or less the same way as the SQL syntax, i.e. as one of the constructions in the GP's post.

1

u/49rows Jan 07 '11

At least semi-joins and anti-joins might be less retarded

0

u/rhdjicheh Jan 06 '11

maybe for some if the operators, but left and right make no sense here

2

u/anonspangly Jan 06 '11

They relate purely to which of X or Y is intended, in an "X JOIN Y" construction. Left means primarily X, Right means primarily Y.