Does having no brackets at “WHERE” produce same results as having brackets? [closed]

Posted on

Question :

Does having no brackets at “WHERE” produce same results as having brackets? I have same result, just wanted to confirm, because from my memory it always best to use rounded brackets.

SELECT `document`.*, `document_info`.`name`
FROM `document` 
LEFT JOIN `document_info` ON `document_info`.`document_id` = `document`.`id`
WHERE `document`.`user_id` = 4358 AND document.status = 4 OR document_info.status > 0

vs

SELECT `document`.*, `document_info`.`name`
FROM `document` 
LEFT JOIN `document_info` ON `document_info`.`document_id` = `document`.`id`
WHERE `document`.`user_id` = 4358 AND (document.status = 4 OR document_info.status > 0)

Answer :

In the order of operations, AND is always evaluated first. So in this case it is the same as:

WHERE (`document`.`user_id` = 4358 AND document.status = 4) OR document_info.status > 0

It is always best practice to just use parentheses anyways. Even if the logic works out the way you want it to, it is much easier to read if you use them (and also properly placed indentation).

Leave a Reply

Your email address will not be published. Required fields are marked *