Joins explained by Venn Diagram with more than one join

CptSupermrkt picture CptSupermrkt · Jul 30, 2012 · Viewed 8.1k times · Source

http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html and http://www.khankennels.com/blog/index.php/archives/2007/04/20/getting-joins/

have been very helpful in learning the basics of joins using Venn diagrams. But I am wondering how you would apply that same thinking to a query that has more than one join.

Let's say I have 3 tables:

Employees

EmployeeID
FullName
EmployeeTypeID

EmployeeTypes (full time, part time, etc.)

EmployeeTypeID
TypeName

InsuranceRecords

InsuranceRecordID
EmployeeID
HealthInsuranceNumber

Now, I want my final result set to include data from all three tables, in this format:

EmployeeID | FullName | TypeName | HealthInsuranceNumber

Using what I learned from those two sites, I can use the following joins to get all employees, regardless of whether or not their insurance information exists or not:

SELECT 
    Employees.EmployeeID, FullName, TypeName, HealthInsuranceNumber 
FROM Employees
INNER JOIN EmployeeTypes ON Employees.EmployeeTypeID = EmployeeTypes.EmployeeTypeID
LEFT OUTER JOIN InsuranceRecords ON Employees.EmployeeID = InsuranceRecords.EmployeeID

My question is, using the same kind of Venn diagram pattern, how would the above query be represented visually? Is this picture accurate?

multiple joins

Answer

Cornelius picture Cornelius · Jul 30, 2012

I think it is not quite possible to map your example onto these types of diagrams for the following reason:

The diagrams here are diagrams used to describe intersections and unions in set theory. For the purpose of having an overlap as depicted in the diagrams, all three diagrams need to contain elements of the same type which by definition is not possible if we are dealing with three different tables where each contains a different type of (row-)object.

If all three tables would be joined on the same key then you could identify the values of this key as the elements the sets contain but since this is not the case in your example these kind of pictures are not applicable.

If we do assume that in your example both joins use the same key, then only the green area would be the correct result since the first join restricts you to the intersection of Employees and Employee types and the second join restricts you the all of Employees and since both join conditions must be true you would get the intersection of both of the aforementioned sections which is the green area.

Hope this helps.