Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Apply filtering conditions in the JOIN ON clause rather than joining then discarding rows
Choose ON versus WHERE by join semantics; inner-join predicate placement is not automatically a performance improvement.
Bad example
| 1 | SELECT a.id, b.name FROM accounts a |
| 2 | LEFT JOIN badges b ON b.account_id = a.id |
| 3 | WHERE b.active = true; |
Explanation (EN)
An outer-join predicate in WHERE can accidentally remove unmatched rows.
Objašnjenje (HR)
Good example
| 1 | SELECT a.id, b.name FROM accounts a |
| 2 | LEFT JOIN badges b ON b.account_id = a.id AND b.active = true; |
Explanation (EN)
This contract retains every account and matches only active badges; use WHERE when dropping unmatched accounts is intentional.
Objašnjenje (HR)
Notes (EN)
Place a predicate according to join semantics: ON controls matching and WHERE filters the joined result, especially for outer joins.
Exceptions / Tradeoffs (EN)
For inner joins many optimizers produce equivalent plans for either location; do not claim a performance win without evidence. Moving a right-side predicate from WHERE to LEFT JOIN ON preserves unmatched left rows and changes results.