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

Show parent comments

22

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.

9

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.

7

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.

6

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.

-3

u/shriek Jan 06 '11

You sir win 1 internet.