SQL Select Statement with WHERE, AND, OR
The way you use parentheses in the description of your goal is correct. The syntax is:
SELECT name, sex, fur, color
FROM dogs
WHERE sex="male" AND fur="short" AND (color="black" OR size="big");
According to Operator precedence for MySQL AND
has higher precedence than OR
.
So
C1 AND C2 OR C3
will be treated as (C1 AND C2) OR C3
To override the default precedence you need to use parenthesis as:C1 AND (C2 OR C3)
In your case the right query is:
SELECT name, sex, fur, color
FROM dogs
WHERE sex='male' AND fur='short' AND (color='black' OR size="big");
Make sure to add parentheses, so the OR condition gets evaluated correctly.
SELECT name, sex, fur, color
FROM dogs
WHERE sex='male' AND fur='short' AND (color='black' OR size='big');