by andy81 · 2y ago · 0 comments
"Vertical join" is normally called union.
"Right join" is just (bad) syntax sugar and should be avoided. Left join with the tables reversed is the usual convention.
The join condition for inner is optional- if it's always true then you get a "cross join". Can be useful to show all the possible combinations of two fields.
I agree that it is seldom good query-writing practice, but I think it makes sense in education because it rounds out the join variations to name them symmetrical, and thus easier to remember.
Now consider b LEFT JOIN a LEFT JOIN c. The result is not the same at all.
A “vertical join” (SQL UNION) is one of the “non-left joins”. How can you transform a “vertical join” to a “left join”?
WITH top_t AS (
SELECT
a
,b
,c
,'top' as nonexistent_col
FROM table_1
), bottom_t AS (
SELECT
a
,b
,c
,'bottom' as nonexistent_col
FROM table_2
)
SELECT
COALESCE(top_t.a, bottom_t.a) AS a
,COALESCE(top_t.b, bottom_t.b) AS b
,COALESCE(top_t.c, bottom_t.c) AS c
FROM top_t FULL OUTER JOIN bottom_t
ON top_t.a = bottom_t.a
AND top_t.b = bottom_t.b
AND top_t.c = bottom_t.c
AND top_t.nonexistent_col = bottom_t.nonexistent_col -- remove this for a normal UNION