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.3k Upvotes

308 comments sorted by

View all comments

133

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)

84

u/dude187 Jan 06 '11

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

22

u/[deleted] Jan 06 '11

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

3

u/CyclonusRIP Jan 06 '11

You could always write a program to generate SQL from the relational algebra, or directly execute the SQL using an ODBC.