There is no notion of having to do things in a certain order. There are consequences of a particular choice of expression.
left join on
returns inner join on
rows plus unmatched left table rows extended by nulls. Similarly, right join on
returns inner join on
rows plus unmatched right table rows extended by nulls. Similarly, full join on
returns inner join on
rows plus unmatched left and right table rows extended by nulls. Always know what inner join on
you want as part of an outer join on
.
After a left
/right
/full
join on
, an inner join on
, where
or having
needing some column(s) of the right/left/2 [sic] table(s) to be not null removes rows with introduced nulls, leaving only inner join on
rows. It "turns OUTER JOIN into INNER JOIN". You are talking about that.
Similarly, after a full join on
, needing some column(s) of only the right/left [sic] table to be not null "turns FULL JOIN into RIGHT/LEFT [sic] JOIN".
You shouldn't have to worry about this. Just focus on writing queries that return what you want. Your question is like asking, should I avoid dividing by zero since it's undefined or adding zero because it doesn't do anything? Why would you, since it doesn't do what you want? If you are writing wrong queries then find out what the operators do.
Is there any rule of thumb to construct SQL query from a human-readable description?
PS My characterization of outer join
s & of when null-extended rows are dropped focuses on the associated inner join
, on the on
as a whole & on whetherleft
and/or right
table columns are null. That's because the simplest definitions of the operators involve only those things. Your choice of organization of parts is misleading & impeding you.
- Any two tables can be
join
ed on
any condition. PK
-FK
equality is just a special case. (PK
s & FK
s are not needed to query. They do imply certain constraints on inputs & results though.) FK
& unique
columns & other columns can have null
s on input. (PK
means unique not null
.)
- "matching columns" & "nothing would match a NULL" are confused because it's row pairs that match or don't, per the whole condition of an
on
.
- "any non-matching rows including NULL from an OUTER JOIN result would be removed when being INNER JOIN"--No, it depends on the whole condition of an
on
or where
.
- "this would be extremely rare"--Doesn't matter. Either something can happen or it can't happen; either the query is wrong or it's right.