SQL AND, OR, NOT
Combine conditions.
Syntax
WHERE (a AND b) OR NOT c
AND needs both sides true, OR needs at least one, NOT flips. AND is evaluated before OR, so use brackets when you mix them.
Example
Query (on the shop database used in the SQL course)
SELECT name, category, price FROM products WHERE (category = 'Electronics' AND price < 100) OR category = 'Books';
Result
| name | category | price |
|---|---|---|
| Wireless mouse | Electronics | 29.0 |
| USB-C hub | Electronics | 49.0 |
| Python Crash Course (book) | Books | 45.0 |
| Deep Learning (book) | Books | 89.0 |
| SQL Pocket Guide | Books | 25.0 |
5 rows
Related
Learn it step by step in the free lesson WHERE: Filtering Rows, with exercises that check your queries.