There is one answer on this question that touches on this.. but I feel it deserves a question of it's own.
This question, which is marked as a duplicate to the first but isn't really, is what I want to ask.. and as it says in the bit:
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
So I'm asking a new question.
I can write a query as:
SELECT *
FROM customer_order co
JOIN customer c
ON c.id = co.customer_id
AND c.type = 'special'
JOIN order o
ON o.id = co.order_id
AND o.status = 'dispatched'
OR:
SELECT *
FROM customer_order co
JOIN customer c
ON c.id = co.customer_id
JOIN order o
ON o.id = co.order_id
WHERE c.type = 'special'
AND o.status = 'dispatched'
I absolutely prefer the first way, especially in more complex queries as it groups the conditions with the tables on which they operate, which makes it easier for me to read and to identify appropriate composite indexes. It also means that if I want to change to a LEFT JOIN
(or maybe RIGHT JOIN
, I don't really use RIGHT JOIN
), all the conditions are in the right place.
There seems to be some preference, however, in the community towards the second way.
Does anybody know if this preference is grounded, perhaps in some performance issue or in some readability issue that I have yet to stumble across? Or can I continue to be a rebel happily?