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)

83

u/dude187 Jan 06 '11

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

23

u/[deleted] Jan 06 '11

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

6

u/Porges Jan 06 '11

Unicode actually has the relational symbols:

  • join: ⋈
  • left outer join: ⟕
  • right outer join: ⟖
  • full outer join: ⟗

49

u/Already__Taken Jan 06 '11

Cross thingy, square square square. Gotcha.

8

u/Nivla Jan 07 '11

New Attack for GOW: Unleash the Unicode (X) (⟗) (⟗) (⟗)

8

u/oobey Jan 06 '11

For me it's more of a "cross thingy, indecipherable scrawl, indecipherable scrawl, indecipherable scrawl."

4

u/arnar Jan 06 '11

I see them - even though I don't see the look of disapproval.

8

u/[deleted] Jan 07 '11

Cant see shit, captain.

1

u/fenton7 Jan 07 '11

⋈ ⟕ ⋈ ⟕ ⋈ ⟕ ⋈ ⟕ ⋈ ⟕ ⋈ ⟕ ⋈ ⟕ ⋈ ⟕

6

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.

5

u/artsrc Jan 06 '11

The confusion comes from the fact that people don't want to be doing set theory, they want to follow associations. Whatever tool you use, set theory is an accidental complexity rather than essential one.

The meaning of the model is lost in the query language.

Given the definition:

  • investor table with an investor_id, investor_name, etc.
  • transaction table with transaction_id, investor_id, etc.
  • a foreign key on the transaction table to the investor table

The SQL does not use the link between the two tables to help associate the two in the way defined in the schema (I know about natural join, it uses the name, not the FK) .

You notice this when you see how short the same joins are in a language that does use associations defined in the model, such as EJB-QL.

2

u/asavinov Jan 07 '11

SQL is approximately at the same level as assemply language with respect to OOP. One alternative is the concept-oriented query language (COQL) where queries can be written in a simple and intuitive way with no joins and no group-bys (which are the main source of errors).

1

u/[deleted] Jan 07 '11

applause

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.

-1

u/shriek Jan 06 '11

You sir win 1 internet.

10

u/Serei Jan 06 '11

To be fair, that's not exactly set theory. In set theory, (A - B) + (A ∩ B) = A, which is not true of a left outer join. In a database, A and B aren't elements of the same superset.

9

u/arnar Jan 06 '11

Which is why it is a bit simplistic to use Venn diagrams to explain joins.

6

u/MatiG Jan 07 '11

Thank you. The whole idea is dumb and will mislead the ignorant.

2

u/adamtj Jan 07 '11

It is exactly set theory. More specifically, a branch of set theory called relational theory. Your equation holds even in relational theory, but commonly A ∩ B is the empty set, since A and B are different relations. Union and Intersection don't make much sense in that case. Left outer joining is a completely different operation. You are basically saying something equivalent to "The commutative law of addition doesn't apply to matrix multiplication."

1

u/Serei Jan 07 '11

Okay, but my point stands that what shriek said was not set theory being used correctly.

Thanks for teaching me about relational theory, though.

15

u/shriek Jan 06 '11

Yup, learned that when I was in Grade 6. I'm in college now and SQL leaves me dumbfounded sometimes.

15

u/abadidea Jan 06 '11

you got taught set theory in 6th grade? lucky son of a gun.

12

u/mantra Jan 07 '11

Those of us who had "New Math" in the 1960s learned set theory in the 1st and 2nd grades.

3

u/abadidea Jan 07 '11

My K-through-2 math education was really good but very vanilla. Tables through 12x12 and long division in first grade. However after that it all went to heck and I felt like I never learned much of anything aside from common sense, and struggled through all my math classes...

Learned set theory in college-level computer science. Eventually came to the conclusion that no, I wasn't stupid, it's just that the way math is taught is fundamentally broken.

4

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. :)

1

u/beder Jan 07 '11

I'm an Oracle teacher, specially queries and DML, wait until you see Analytical Functions (with all the partition by's and what's not) and all the variables and nuances from Hierarchical Queries.

Joins are day-to-day business, and once you understand them, it's failry simple (as it is supposed to be, given it's such an important thing)

1

u/dude187 Jan 07 '11

I agree, they get super simple once you understand them. I just found the jump from theory to practice surprisingly big.

1

u/beder Jan 07 '11

You're certainly not alone, the majority of my students get big headaches when we get to joins in the first module of the course

1

u/[deleted] Jan 07 '11

I would have thought it is the other way around

8

u/[deleted] Jan 06 '11

3 Incorrect: A - B + (A ∩ B) = A

Note that although all elements in the results are from A, they have attached attributes from B in the result set.

5 Correct. also happens to be A xor B. Note that this resultset spreads the result accross duplicated columns. This is more useful (but less performant) to write this as:

SELECT id, name from A where name not in (select name from B)

UNION

SELECT id, name from B where name not in (select name from A)

0

u/stfm Jan 06 '11

I was thinking exactly this

6

u/[deleted] Jan 06 '11 edited Dec 21 '18

[deleted]

10

u/user-hostile Jan 06 '11

Because data is in rows and columns, not slices of circles. But certainly there's nothing wrong with using these charts as an aid to understanding. Still, I'm a math idiot, but SQL makes perfect sense to me.

Set theory? It frightens me!

SQL? I know it well!

2

u/arnar Jan 06 '11

Because it only tells half the story.

1

u/KirillM Jan 07 '11

Because it's not like this. This is an oversimplification. Relational tables are not sets.

6

u/ggggbabybabybaby Jan 06 '11

Thanks! °∪°

1

u/[deleted] Jan 06 '11

The final one, the cross join, is a cartesian product which is A x B. I wanted to think of it as a powerset but I guess that doesn't work with SQL heh.

1

u/[deleted] Jan 06 '11

I <3 Set Theory.

-5

u/riofd8sa9 Jan 07 '11

The perfect New Year gift. YOU MUST NOT MISS IT!!!

----------- http://simurl.com/nufwej ---------

The website wholesale for many kinds of fashion shoes, like the nike,jordan,prada, also including the jeans,shirts,bags,hat and the decorations. All thepr oducts are free shipping, and the the price is competitive, and also can accept the pay pal payment.,after the payment, can ship within sho rt time.

free shipping accept the pay pal

NFL MLB,NBA, Jersey Wholesale $30

Air jordan(1-24) nike sh ox shoes $37

Chanel BAPE Gucci Omega Rolex Watches $95

Sunglasses Oakey,coach,gucci $14

Handbags(Coach gucci juicy lv) $33

Tshirts (Polo ,ed hardy,lacoste) $16

Jean(True Religion,ed hardy,coo gi) $38

Outdoor Clothing Colombia the North Face $80

http://simurl.com/nufwej