Filtering in SQL JOIN clause vs WHERE clause impacts correctness and intent

A SQL concept that became much clearer to me recently: Filtering in the JOIN clause vs the WHERE clause isn’t just about syntax; it directly impacts correctness, intent, and sometimes performance. Consider this: -- Case 1 SELECT * FROM orders o LEFT JOIN payments p ON o.id = p.order_id WHERE p.status = 'success'; vs -- Case 2 SELECT * FROM orders o LEFT JOIN payments p ON o.id = p.order_id AND p.status = 'success'; At first glance, these look similar, but they behave very differently. Case 1 filters after the join → removes NULLs → behaves like an INNER JOIN Case 2 filters during the join → preserves unmatched rows → true LEFT JOIN behavior The deeper insight for me: In many INNER JOIN scenarios, query optimizers can push filters around, so performance may look identical. But with OUTER JOINs, filter placement defines intent, and the optimizer cannot always “fix” a logically incorrect query. So the real question isn’t: “Which is faster?” It’s: At what stage do I want this filtering to happen? During matching? → use ON After matching? → use WHERE That mental model made SQL much easier to reason about for me. Curious.. how do you usually think about this when writing joins? #SQL #DataAnalytics #DataEngineering #Learning

  • icon

To view or add a comment, sign in

Explore content categories