Right-Side Conditions in a Left Join

A condition in ON affects which right rows are matched. A later WHERE condition can remove the already produced null-extended rows.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace published entries
  2. Test a revealing category
  3. Compare condition placement on a parent with an inactive child
  4. Tie the query to the report's question

Trace published entries

To list all categories and only matched published articles, place the publication condition in the join rule. A WHERE condition requiring a true article publication flag can eliminate empty categories. A left join's unmatched row explains the missing fields.

Test a revealing category

Use a category with draft entries and no published ones. Check whether the expected result retains the category with no matched article. Consult WHERE behavior for which predicates keep results.[1]

Compare condition placement on a parent with an inactive child

Branch one has an active office and branch two an inactive one. Putting active status in the join condition preserves branch two as unmatched. Putting it in WHERE after joining removes branch two from the result. See row filtering.

Filter in ON: branch 1 with office, branch 2 with null office
Filter in WHERE: only branch 1
A branch with no office at all also needs an explicit test

Tie the query to the report's question

For all branches and their qualifying offices, require preservation of every branch. For only branches with an active office, an inner join or existence check may state the intent more directly.

References

  1. PostgreSQL Global Development Group, Queries ↩