Isn't SQL A left join B, just A?
Asked Answered
W

6

50

I was looking at a few graphs to understand the difference between the joins, and I came across this image:

enter image description here

Maybe the problem is in representing this with Venn Diagrams.

But looking at the first join, top left, isn't that just A?

What difference does B make?

Watchword answered 27/12, 2018 at 18:16 Comment(6)
No, it's A and part of B, just as the diagram suggests.Sarasvati
This set of Venn diagrams is a bit misleading for describing SQL joins. It doesn't show how rows in A can be duplicated when there are multiple matches on the join condition in B.Inequitable
@Inequitable exactly how can the sql joins be turned as a mathematical venn diagram as an explanation. It is certainly misleading because it doesnt take all instances to cover all alternatives of it. Perhaps looks like a shortcut document for clearing an interview on the topic called basic sql joinsWindhover
@HimanshuAhuja I'd probably opt to describe them with something like this: blog.jooq.org/2016/07/05/…Inequitable
Possible duplicate of What is the difference between "INNER JOIN" and "OUTER JOIN"?Yep
Such Venn-like diagrams for joins are unclear, unhelpful & misleading. See my Q&A comments here. "All you have to do to see this is try to write a correct legend for one." Don't forget that SQL tables are bags not sets. Joins are on any condition & don't need constraints so at best the diagrams when explained address special cases and/or partial properties. And explanations are very complicated, hence useless to teach inner or outer join or their difference.Yep
B
47

No, since it's a join, it can produce cardinalities greater than 1 for the rows in A. That is, if there are multiple matching rows from B then a row in A will show up multiple times.

Example:

Table A:

id name
-- -------
 1 Alice
 2 Malcolm
 3 Kelly

Table B:

id_a preferred_food
---- --------------
   1 Pizza
   2 Burger
   2 Steak
   2 Minestroni

Then "A left join B" will give you:

id name    id_a preferred_food
-- ------- ---- --------------
 1 Alice      1 Pizza
 2 Malcolm    2 Burger
 2 Malcolm    2 Steak
 2 Malcolm    2 Minestroni
 3 Kelly   null null

In short:

  • All rows from A show up in the left join: even 3 Kelly shows up.
  • Columns from B will show up with nulls when there are no matching rows in B: row 3 Kelly has null in the last two columns.
  • Rows in A may show up multiple times when they have multiple matches in B: row 2 Malcolm shows up three times.
Bickel answered 27/12, 2018 at 18:27 Comment(0)
C
3

Your diagram isn't quite a Venn diagram.

The intersection of the two circles represents joined rows (according to your join condition) with data from both table A and table B.

The left crescent (labeled "A") represents rows in table A that do not have any corresponding rows in table B; the right crescent (labeled "B") represents rows in table B that do not have any corresponding rows in table A.

What the top left diagram is supposed to show is that a left join gives you data from both table A and B that can be joined up according to your join condition, plus all rows from table A that have no corresponding match in table B.

Covenant answered 27/12, 2018 at 18:42 Comment(1)
The diagram is a Venn diagram if we take the circles A & B to be the rows of A left join B & A right join B respectively. But the diagram is poor. See my comment on the question & the comments & answers it mentions.Yep
M
2

With JOINs, you get fields from both tables, not only A. It also multiplies the number of records returned if the relationship between tables is not 1-1.

Basically, the only difference between all those JOIN is the behavior when records in A do not match any records in B and vice versa. Your diagrams only show that behavior.

Diagram top let = keep all the records from A, whether or not they match to anything in B (+ what is not represented: when they match, get the data from B).

Minna answered 27/12, 2018 at 18:20 Comment(0)
W
1

A LEFT JOIN B MEANS A + (common ENTRIES OF A WHICH ARE THERE IN B) left join would rather be performed where the table which is left joined has more records in this case A and those records might refer some other columns from B on some common condition. hence, the result A left join B would not mean only A but columns of B added as well in case if required on some common value of A and B.

Windhover answered 27/12, 2018 at 18:22 Comment(0)
S
1

In that diagram they omit the word 'outer' from that description, but you should read it as LEFT OUTER JOIN.

The table to the "left" of the SQL statement (which is going to be the table after the FROM, and in this case, table A) will have every row returned whether or not there is a corresponding row in Table B that matches the JOIN condition.

This is the difference between an inner join and an outer join. Inner joins only return a row where there is a match on the join condition, whereas, a LEFT OUTER join returns the same rows returned by an INNER join, as well as a ROW for any rows in the LEFT table that don't satisfy the JOIN condition. For those rows in the LEFT table that don't JOIN to one or more rows in Table B, the columns of table B that might be specified in the SELECT will be NULL.

You can see that this property of unmatched LEFT rows having NULL values for table B columns is used in the example directly below to filter out any rows derived from a successful join to table B.

Storehouse answered 27/12, 2018 at 18:57 Comment(0)
T
0

No, SQL A left join B, isn't just A.

In the Venn diagram depicted in the OP, the first join (top left) is a left join. A left join returns all rows from the left table (A), and the matching rows from the right table (B). If there are no matching rows in the right table, the left table rows are returned with NULL values for the columns from the right table.

In the Venn diagram, the left table is represented by the circle on the left, and the right table is represented by the circle on the right. The intersection of the two circles represents the rows that are in both tables. The left join returns all of the rows in the left circle, and the matching rows in the right circle. The rows in the left circle that do not have a matching row in the right circle are returned with NULL values for the columns from the right table.

So, in the Venn diagram, the top left join returns all of the rows in the left circle, which is the table A. The rows in the right circle that do not have a matching row in the left circle are not returned.

The confusion may arise when looking at a Venn diagram representation of a left join. Venn diagrams are typically used to represent set operations like union, intersection, and difference, which can be related to SQL operations like UNION, INTERSECT, and EXCEPT. However, when it comes to joins, Venn diagrams may not accurately represent the concept.

In a Venn diagram, the overlapping region represents the intersection of sets. But in a left join, the overlapping region would actually represent the matching rows between table A and table B. The left table (A) would be represented by a circle, and the right table (B) would be represented by another circle. The overlapping region would show the rows that have a match based on the join condition.

To summarize: Venn diagrams are OK as long as they make clear that the graphic representation of UNION, INTERSECT, EXCEPT set operations to describe a JOIN, refer to the cartesian products of table A and table B, not to the table themselves.

Triode answered 22/5, 2023 at 8:43 Comment(2)
This is unintelligible before & after "No, SQL A left join B, isn't just A", including any reasoning trying to justify that. If there is something sensible you're trying to say, you're not saying it. PS Venn Diagram for Natural Join Also see my comments on the question & Q&A they reference.Yep
"In the Venn diagram, the left table is represented by the circle on the left, and the right table is represented by the circle on the right." Not so. "The intersection of the two circles represents the rows that are in both tables." Not so. "The left join returns all of the rows in the left circle, and the matching rows in the right circle." Input rows appear as result subrows, but not rows, and the post does not define just how output is a function of input. Etc etc. Anyway tables are not sets of rows, they are bags of rows. Etc etc. And that diagram is misleading, unclear & not useful.Yep

© 2022 - 2024 — McMap. All rights reserved.