AND and OR
- SQL course
- Beginner
- Lesson 9/115
- 4 min
Combine multiple conditions with AND and OR.
AND and OR
To combine conditions you use AND and OR. AND requires both conditions to hold; OR needs at least one to hold.
SELECT name FROM products
WHERE price > 10 AND stock > 0;This returns products with price over 10 and stock over 0.
name is selected from products. WHERE keeps only the rows where price > 10 AND stock > 0 holds.
AND and OR — details
When AND and OR appear together, parentheses matter. AND is evaluated before OR; use parentheses to make the order explicit and safer.
For example, WHERE (city = 'İzmir' OR city = 'Bursa') AND active = 1 returns only active customers in those two cities thanks to the parentheses.
Practice AND and OR
Select name from products where price is greater than 10 AND stock is greater than 0.
Starter code
-- join two conditions with AND
SELECT name
FROM products
WHERESample solution
SELECT name
FROM products
WHERE price > 10 AND stock > 0Expected output
name Defter Çanta
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 AND and OR: match the terms
| Term | Meaning |
|---|---|
AND |
both |
OR |
at least one |
NOT |
negation |
parentheses |
precedence |
2 Join with AND
SELECT name FROM products
WHERE price > 10____stock > 0;
SELECT name FROM products
WHERE price > 10 AND stock > 0;
Explanation: AND returns a row only when both conditions are true.
3 If one AND condition is false, what happens?
- The row is filtered out Correct answer
- Raises an error
- The row is returned
- All rows are returned
Explanation: AND requires both conditions.