2

The following image can be found by searching for SQL join on the internet:

enter image description here

Can we interpret this as a Venn diagram, in the following way?

  • The circle labelled Table A is the set of records in Table A
  • The circle labelled Table B is the set of records in Table B
  • The intersection of the sets represents inner join (aka just join in SQL)

Motivation

The diagram shown, which appears in various forms widely on the net, looks awfully like a Venn diagram. So the first intuition is to think of it as a Venn diagram. However, one might get stuck trying to define the contents of the sets "Table A" and "Table B".

There is also information on the internet that appears to conflict. This question hopes to help resolve some of the conflicts that arise. Some sites argue that we shouldn't use Venn diagrams for explaining joins:

While there is other information that uses Venn diagrams between tables A and B to explain joins on those tables e.g.

philipxy
  • 14,867
  • 6
  • 39
  • 83
Colm Bhandal
  • 3,343
  • 2
  • 18
  • 29
  • Consider flagging the question with your bonus to ask a moderator to move the bonus to here (or to just cancel it to not make a mess there). But clearly explain. You could reference [this meta question](https://meta.stackoverflow.com/a/296493/3404097). But this question is only asking about particular interpretations of 1 Venn labelling, while there's no reason to think that those are even interpretations anybody ever used in writing or reading. So it's not clear that this is yet the question you want answered. And there is a valid Venn for inner vs outer. Good luck. – philipxy May 08 '20 at 19:54
  • 1
    Thanks @philipxy. It was your comments that made me see that light: a bonus on that already cluttered question was probably a bad idea. However, I am reluctant to bring a bonus to my own question as it seems a little attention grabbing. The question is alredy answered, so it would be like saying "Hey guys, look at my great answer! Please upvote it so I can claw back some of the rep I lost on the bonus" Also, there are other questions I want to ask and answer on this subject. This is certainly not the end. – Colm Bhandal May 09 '20 at 07:37

1 Answers1

2

No. The diagram cannot be interpreted as a Venn diagram if it is to represent joins. Let's show why with a counter-example. Let's choose a minimal counterexample to avoid clutter. As such, let's assume both tables have only one column: ID. Furthermore, let's assume that both of the tables have just two records in them, and they both have the same two records: just the IDs 1 and 2. So both tables just look like this:

enter image description here

Now, suppose we run the following SQL:

select * from A join B

Since there is no ON clause, we will get back the table of all combinations of records in A with all records in B. Since there are 2 records in each, we will get back 4 = 2 * 2 records. Our resultant table from the select will look like this, with A and B's ID columns fully qualified by the table name:

enter image description here

However, the intersection of the two sets of records, as depicted in the Venn diagram, is not equal to this, no matter what interpretation we choose for records.

Interpretation #1: Each record is just a tuple of values. In this case, since we only have one column in each table, our tuples boil down to just single values. Since our ID column is numeric, this interpretation boils down to a record being just a numeric ID. The intersection of the sets is the intersection of the set {1, 2} with itself. Which is just the original set. Or as a table, the intersection would be:

enter image description here

Interpretation #2: Each record from Table A is a thing completely distinct from any record in Table B, or indeed any other record in Table A itself. In this case, since no two records are the same thing, the intersection of the two sets will be empty i.e. the null set. So as a table it certainly would not be the same as the result of the join.

This concludes the proof that this exact interpretation of a Venn diagram cannot be considered to represent a SQL join.

Colm Bhandal
  • 3,343
  • 2
  • 18
  • 29