I don't believe I can help too much with that I'm sorry to say. Certainly as far as such a specific requirement goes for resources.
However, let me see if I can explain it in a way that makes sense :
In the WHERE clause you have what is essentially Boolean arithmetic - except it's really just ordinary arithmetic that is eventually converted to Boolean in order to be processed by the SQL engine.
In short, any numeric result of zero is treated as False whereas any other result is treated as True. So :
will fail if the
Blah blah blah
part is zero but succeed otherwise.
AND
&
OR
are actually Boolean operators so they can be used in arithmetic but are specifically Boolean.
AND
takes all the bits (Binary DigITs) from one side, and all those form the other, and leaves a zero for any bit in the result where either side has a zero.
OR
takes all the bits from one side, and all those form the other, and leaves a one for any bit in the result where either side has a one. That's what is technically going on under the hood.
Just as with ordinary arithmetic, the operator works on the values on either side of it and produces a result (EG. 1 + 2 = 3) so
True AND False = False
&
True OR False = True
. All well and good if there is only the one operator but when more are involved we have to remember the order of precedence. For Boolean operators the
NOT
is highest, followed by
AND
& then
OR
. Parentheses come higher than anything else of course, and if all else is equal then you process the order from left to right.
How does that help us? Well, each operator has a resultant value on each side of it with the exception of
NOT
which only has a single operand to its right.
NOT
is a very simple operation where each bit is switched from its existing value to the other (of zero & one). When all the operators have been processed then the result of the last operation is the value that determines whether or not that particular record is included in the output or not.
That all may sound daunting and complicated so let's explore a couple of illustrative examples. I'm only interested in :
- Boy children between the ages of 5 and 15.
- WHERE [Gender]='M'
-
AND [Age] Between 5 And 15
No need for parentheses here. - Boy children between the ages of 5 and 15.
- WHERE NOT([Gender]='F'
-
OR [Age]<5
-
OR [Age]>15)
We need the NOT
to be applied after all the other operators so we use parentheses. - Boy children between the ages of 5 and 15.
- WHERE [Gender]='M'
-
AND NOT([Age]<5
-
OR [Age]>15)
We need the NOT
to be applied after the OR
operator so we use parentheses.
Remember that the WHERE clause is applied to each record as it is processed so the criteria is not the same as you would express it for the group as a whole. "I want all men from India and Pakistan." doesn't mean all men who are from both India & Pakistan. It means all men who, individually, are either from India OR Pakistan. So, the criteria would use the
OR
operator. Not the
AND
operator :
- WHERE [Country]='India'
-
OR [Country]='Pakistan'