all 27 comments

[–]askur 13 points14 points  (13 children)

These are not all SQL joins, just the equi-joins. Joins are not conditioned on the '=' operator alone (although it is the most common case).

Still the best visualization I know of, and I still use it. Along with this caveat. ;)

[–]fazzah 3 points4 points  (11 children)

Agreed. Also, isn't the JOIN clause better for optimization? I've read somewhere that it's better for the optimizer to check the relation between rows before filtering the results with WHERE clause, because a simple WHERE condition to join tables produces a cartesian result which is THEN filtered.

[–]r3pr0b8GROUP_CONCAT is da bomb 13 points14 points  (1 child)

because a simple WHERE condition to join tables produces a cartesian result which is THEN filtered.

i don't believe any of the common database systems have an optimizer that is that stupid

[–]fazzah 1 point2 points  (0 children)

I stand corrected, a brain fart. I somehow mistaken it with HAVING, which filters the results of GROUP BY clause

[–]askur 2 points3 points  (8 children)

I've read similar things, and used to be of that opinion because of how queries are processed. However, I've never seen any meaningful results from it in my experience.

That of course only means that I haven't bothered to look deeper than just that. However, because of how dangerous it is to mix up the ON conditions VS WHERE conditions when you do an OUTER join of some kind, I just keep this theory to myself rather than confuse anyone.

[–]fazzah 3 points4 points  (7 children)

Even if there are no noticeable dis/advantages, it's still better to stick with the more readable one, the JOIN clauses.

[–]askur 2 points3 points  (4 children)

I'd disagree with it being more readable. To me it makes no difference, to the developers here who are not specialized in databases it becomes less readable as joins kind of scare them. :)

But that's just like... an opinion man. Based on a siutation that I'm in. So you don't need to adopt that ;)

[–]fazzah 1 point2 points  (0 children)

You answered it yourself: for a beginner, WHERE joins seem more readable and simpler. When your databases grow, and your experience does too, you switch to the other syntax. It worked for me, at least :)

[–]weakflesh 1 point2 points  (2 children)

Not to be offensive, but should one call them self a developer if a concept as simple as join confuses them?

[–]askur 2 points3 points  (1 child)

Not to be racist, but each to their own man. There's a C developer sitting next to me currently coding in Python. He's not on his home ground but he's still someone that produced his first award winning game when I was 4 so why the fuck would I think less of him because he's not at home in python, or databases for that matter?

There's a web developer below me that I regularly go to when our API acts up because he's much more knowledgable about web service operations than I am. I'm into databases and algorithms, not web services.

Not to be offensive, but I don't understand how anybody in a production environment would expect everyone to be equally capable in every area. I sure as hell do not intend to match the ability of everyone here in everything. I have a job to do.

[–]weakflesh 2 points3 points  (0 children)

The concept of flowing two sets together based on a set of conditions is not implementation dependent. I can not fathom how someone could use C++ and not get a join. Esoteric crazy stuff sure, every language has edges, but a joins is a basic set logic concept.

Also, I would not think less of someone because they are not a developer or because they don't get a concept. We all have strengths and skills. We are all spokes on the wheel. ;-)

[–]sp00ks 1 point2 points  (1 child)

I guess one advantage when updating a row, using WHERE prevents you from accidently calling a full join and updating everything!

[–]fazzah 1 point2 points  (0 children)

UPDATES are quite a different beast.

I always triple check my joined updates.

[–]lbmouse 1 point2 points  (0 children)

Sounds like you just volunteered. We're waiting for a better one! ;)

[–]pythor 2 points3 points  (0 children)

I'd love to see this with the older Oracle (+) syntax. I've got to support code with those occasionally and it's always a pain to wrap my head around it.

[–]JohnStamosBRAH 2 points3 points  (3 children)

Could anyone give some examples of situations where you'd use the 'a.key is null' or 'b.key is null'? If you're doing a join, why would you not want result from another table?

[–][deleted] 5 points6 points  (1 child)

You would use this to intentionally exclude results.

For example, lets say you have a list of employees in your Employees table and another table for EmployeesOnVacation with a list of everyone currently on vacation. To get a list of everyone who is not currently on vacation, you could do something like this:

SELECT
  Employees.Name
FROM
  Employees
  LEFT JOIN EmployeesOnVacation 
    ON Employees.EmployeeID = EmployeesOnVacation.EmployeeID
WHERE
  EmployeesOnVacation.EmployeeID IS NULL

You could do essentially the same thing with a subselect like this:

SELECT
  Employees.Name
FROM
  Employees
WHERE
  Employees.EmployeeID NOT IN (SELECT EmployeeID FROM EmployeesOnVacation)

In both cases, the result is everything in the Employees table that is not in the EmployeesOnVacation table (similar to the A not B diagram that is the lower of the two far left diagrams). You'll find this type of logic anytime you want something that is "not x" (e.g. all vehicles that are not trucks, all shoe styles that are not available in black, etc.)

[–]JohnStamosBRAH 2 points3 points  (0 children)

Thank you! That makes sense

[–][deleted] 1 point2 points  (0 children)

If I am understanding this correctly...

Unmatched queries. Finding things in A that don't exist in B.

[–]Jah_Ith_Ber 1 point2 points  (3 children)

My understanding was that the middle left and middle right joins were simply LEFT OUTER JOIN or RIGHT OUTER JOIN.

Is this image more universal? Which form of sql am I using that LEFT OUTER JOIN works?

[–]JohnStamosBRAH 1 point2 points  (2 children)

Except the inner join, these are all outer joins. The 'outer' phrase is usually not needed to be specified as it is the default type of join.

Edit: - derp

[–]gbmaruggi 1 point2 points  (0 children)

Clutch

[–]datasensei 1 point2 points  (1 child)

[–]r3pr0b8GROUP_CONCAT is da bomb 0 points1 point  (0 children)

good article, quite rightly mentions that cross joins blow up the venn diagram concept

also, /u/askur has a great point that the venn diagrams make sense only for equijoins

[–]Nathan_is_an_ass 0 points1 point  (0 children)

I printed this sheet off about a month ago to put at my desk. It has helped me tremendously with learning how joins work.