AND, OR, and NOT
Several conditions can be combined
When one comparison is not enough, conditions are combined with logical operators. AND requires both parts to be satisfied, OR requires at least one, and NOT negates a condition.
SELECT name, price
FROM products
WHERE price >= 1000
AND available = TRUE;
Only available products priced at 1000 or more appear in the result. If either part of the condition is not satisfied, the row is excluded.
Parentheses make logic explicit
SELECT name
FROM products
WHERE (category = 'book' OR category = 'course')
AND available = TRUE;
The parentheses make it explicit that either of the two categories is acceptable first, and then availability is additionally required for those rows.
Do not rely on memory of operator precedence.
AND normally has higher precedence than OR. When an expression mixes them, parentheses almost always make the intention clearer and reduce the risk of mistakes.
Check yourself
Exercise review
The mentor knows the solution and will help you reach it independently
Sign in to use the Mentor
The Mentor uses the current exercise and your conversation history.
Sign in
Review the key nuances and the answer after making your own attempt.
Sign in to view the solution
The solution and explanation are available after signing in.
Sign in