-
-
Save iampatgrady/eb377267b9ab2570000cdfc172248516 to your computer and use it in GitHub Desktop.
SQL joins: all in one
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- create two tables: L and R | |
-- content of tables a "val" column with two rows. | |
-- rows in L: "left only" and "both" | |
-- rows in R: "right only" and "both" | |
with l as ( | |
select 'both' as val | |
union | |
select 'left_only' as val | |
), r as ( | |
select 'both' as val | |
union | |
select 'right_only' as val | |
) | |
-- now do all kinds of joins and check the result. | |
select 'no join' as jointype,l.val as lval, r.val as rval from l,r | |
union | |
select 'join' as jointype,l.val as lval, r.val as rval from l join r on l.val = r.val | |
union | |
select 'left join' as jointype,l.val as lval, r.val as rval from l left join r on l.val = r.val | |
union | |
select 'right join' as jointype,l.val as lval, r.val as rval from l right join r on l.val = r.val | |
union | |
select 'full join' as jointype,l.val as lval, r.val as rval from l full join r on l.val = r.val | |
union | |
select 'inner join' as jointype,l.val as lval, r.val as rval from l inner join r on l.val = r.val | |
union | |
select 'plus left side' as jointype,l.val as lval, r.val as rval from r,l where l.val(+) = r.val | |
union | |
select 'plus right side' as jointype,l.val as lval, r.val as rval from l,r where l.val = r.val(+) | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment