by notoriousarun on 7/7/21, 12:07 PM with 53 comments
by twic on 7/7/21, 2:49 PM
Firstly, joins are substantially orthogonal to keys. You can join two relations with no keys at all. Keys might help you make sure that there are going to be results to your join, but they are not required, and i wouldn't start with them.
Secondly, union is not a join. If mentioned at all, it should be in an addendum about other ways to combine tables.
Thirdly, left and right joins are specific kinds of outer join. Surely it's much clearer to describe "left, right, and full outer joins" than "left, right, and outer joins".
Fourthly, surely the most logical way to explain joins is the progressive refinement cross -> inner -> equi -> natural -> full outer -> left/right outer. A cross join is a Cartesian product, simple to explain. Restrict that with a predicate and you have an inner join (i'd use an example where the predicate is not equality). Introduce ON as a synonym for WHERE in inner joins. Make the predicate equality on a set of the columns and it's an equijoin. Introduce USING as a shorthand for ON in equijoins. Make that set all the corresponding columns and it's a natural join. Now include all the rows that didn't match and it's a full outer join. Note that WHERE wouldn't work here. Now just the rows on one side or the other and it's a left/right outer join.
by radiospiel on 7/7/21, 1:32 PM
Any JOIN is what the author is calling a "CROSS JOIN", with "ON" (which is optional) basically being a WHERE condition, and "LEFT", "RIGHT", "INNER", "OUTER" words only describing whether or not to drop a result if the left/right side of the join would be a NULL reference.
And, of course, a "UNION JOIN" is not a join at all, because nothing is joined.
by magicalhippo on 7/7/21, 2:55 PM
A contrived example for illustration:
select oh.oid, oh.order_ref, ol.num_lines, ol.total_value
from order_head oh
join (
select oid, count() as num_lines, sum(value) as total_value
from order_lines
where status = 'ready'
group by oid
) ol on ol.oid = oh.oid
where total_value > 500
For certain queries this has led to huge speed-ups, by allowing rows to be filtered sooner rather than post-massive-join. For others it makes it easier to integrate with libraries, as aggregate fields like in the example can be filtered using the main where clause rather than in a having clause.by Diggsey on 7/7/21, 1:03 PM
by contravariant on 7/7/21, 12:44 PM
You'll want to use "UNION ALL" if your intent is to concatenate the two sets of rows, and not to calculate the (deduplicated) union. If only because it's much faster if your database doesn't have to check for duplicates.
by ProAm on 7/7/21, 2:32 PM
[1] https://blog.codinghorror.com/a-visual-explanation-of-sql-jo...
by major505 on 7/7/21, 3:06 PM
Still the best tutorial.
by hactually on 7/7/21, 12:46 PM
Props to the author
by flowerlad on 7/7/21, 1:40 PM
by 725686 on 7/7/21, 2:51 PM
by statmapt on 7/7/21, 1:21 PM
What framework/service is being used to build it?
by b0tch7 on 7/8/21, 5:59 AM
by AtillaBosma on 7/7/21, 12:47 PM
Thanks for the learning resource.
by ohashi on 7/7/21, 12:48 PM
by jackfoxy on 7/7/21, 1:56 PM
by killingtime74 on 7/7/21, 10:46 PM