3
u/pythor Feb 11 '14
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.
3
u/JohnStamosBRAH Feb 12 '14
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?
6
Feb 12 '14
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 NULLYou 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.)
3
2
Feb 12 '14
If I am understanding this correctly...
Unmatched queries. Finding things in A that don't exist in B.
2
u/Jah_Ith_Ber Feb 11 '14
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?
2
u/JohnStamosBRAH Feb 12 '14 edited Feb 12 '14
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
0
2
2
u/datasensei Feb 11 '14
Related Coding Horror blog post: http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
1
u/r3pr0b8 GROUP_CONCAT is da bomb Feb 11 '14
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
1
u/Nathan_is_an_ass Feb 12 '14
I printed this sheet off about a month ago to put at my desk. It has helped me tremendously with learning how joins work.
1
u/msi77 Apr 28 '14
Explicit join operations (flash): http://www.sql-tutorial.ru/en/book_explicit_join_operations.html
13
u/askur Feb 11 '14
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. ;)