I have this query:
SELECT * FROM (`users`) WHERE `date_next_payment` <= \'2011-02-02\'
AND `status` = \'active\' OR `status` = \'past due\'
It's because when you are not using the parentheses you actually saying :
`date_next_payment` <= '2011-02-02' AND `status` = 'active'
OR
`status` = 'past due'.
Which means that when status` = 'past due' this record would be shown too. regarless of passing the other conditions.
But when you do use parentheses you require:
`date_next_payment` <= '2011-02-02'
AND to pass one of the two other conditions.
This is because OR has lower operator precedence than AND. Whenever the DB sees an expression like
A AND B OR C
the AND is evaluated first, i.e. it is equivalent to
(A AND B) OR C
So if you explicitly want
A AND (B OR C)
instead, you must put in the parentheses.
This is btw not specific to SQL. The order of precedence of these operators is the same in all programming languages I know (i.e. at least C, C++, C#, Java and Unix shell scripts).