SQL has defined logical precedence:
NOT, thenAND, thenOR. Parentheses make the intended grouping explicit and easier to verify.
1. Introduction
Logical operators are used to combine or modify conditions in SQL.
They are used inside the WHERE clause when you want to apply more than one rule.
In real-world SQL, filtering is rarely based on just one condition.
Most business queries need multiple conditions such as:
- users from India and active
- products cheaper than 100 or on discount
- all customers except blocked users
This is where logical operators become important.
2. What Are Logical Operators?
Logical operators allow SQL to combine multiple conditions and decide whether a row should be included in the result.
They help SQL answer questions like:
- Should all conditions be true?
- Should at least one condition be true?
- Should this condition be reversed?
These decisions are made using three operators:
ANDORNOT
3. Types of Logical Operators
| Operator | Meaning |
|---|---|
AND | All conditions must be true |
OR | At least one condition must be true |
NOT | Reverses / negates a condition |
These are the three core logical operators used in SQL.
4. Real-World Example — countries Table
We will begin with the countries table.
| country | population | area | region |
|---|---|---|---|
| India | 1400000000 | 3287000 | Asia |
| China | 1200000000 | 4287000 | Asia |
| Brazil | 214000000 | 8516000 | South America |
| Australia | 25700000 | 7692000 | Oceania |
| USA | 331000000 | 9834000 | North America |
5. AND Operator
Definition
AND means all conditions must be true.
A row is returned only if every condition passes.
If even one condition fails, the row is removed.
Query
SELECT *
FROM countries
WHERE population > 50000000 AND area < 5000000;Explanation
A country must satisfy both conditions:
- population > 50M
- area < 5M
Only rows passing both rules are returned.
Output
| country | population | area | region |
|---|---|---|---|
| India | 1400000000 | 3287000 | Asia |
| China | 1200000000 | 4287000 | Asia |
Mental Model
AND means:
This condition and that condition must both be true.
It is strict filtering.
6. OR Operator
Definition
OR means at least one condition must be true.
A row is returned if it satisfies any one of the conditions.
It is broader and more flexible than AND.
Query
SELECT *
FROM countries
WHERE region = 'Asia' OR region = 'Oceania';Explanation
Return countries that are either:
- in Asia
- or in Oceania
A row only needs to match one condition.
Output
| country | population | area | region |
|---|---|---|---|
| India | 1400000000 | 3287000 | Asia |
| China | 1200000000 | 4287000 | Asia |
| Australia | 25700000 | 7692000 | Oceania |
Mental Model
OR means:
This condition or that condition can be true.
It broadens the result.
7. NOT Operator
Definition
NOT reverses a condition.
It returns rows where the condition is false.
This is used when you want to exclude something.
Query
SELECT *
FROM countries
WHERE NOT region = 'North America';Explanation
Return all rows except those in North America.
This removes USA.
Output
| country | population | area | region |
|---|---|---|---|
| India | 1400000000 | 3287000 | Asia |
| China | 1200000000 | 4287000 | Asia |
| Brazil | 214000000 | 8516000 | South America |
| Australia | 25700000 | 7692000 | Oceania |
Mental Model
NOT means:
Show me everything except this.
8. Deep Dive — Combining Multiple Conditions
In real-world projects, queries usually involve multiple business rules.
Examples:
- customers who signed up recently and spent more than 100
- products in Electronics or Accessories
- all users except blocked ones
Logical operators allow SQL to combine these rules and filter very specific rows.
This is one of the most practical SQL concepts in real projects.
9. Real-World Example — products Table
We will now use the products table.
| product_id | product_name | category | price | stock_quantity |
|---|---|---|---|---|
| 101 | Gaming Mouse | Accessories | 50 | 100 |
| 102 | Mechanical Keyboard | Accessories | 120 | 50 |
| 103 | Curved Monitor | Electronics | 300 | 20 |
| 104 | Ergonomic Chair | Furniture | 250 | 10 |
| 105 | USB-C Cable | Cables | 15 | 200 |
10. Case 1 — AND for Strict Filtering
Query
SELECT product_name, category, price
FROM products
WHERE category = 'Accessories' AND price > 80;Explanation
A row must satisfy both:
- category = Accessories
- price > 80
Only one product passes both conditions.
Output
| product_name | category | price |
|---|---|---|
| Mechanical Keyboard | Accessories | 120 |
11. Case 2 — OR for Flexible Filtering
Query
SELECT product_name, category
FROM products
WHERE category = 'Furniture' OR category = 'Cables';Explanation
A row is included if category is:
- Furniture
- or Cables
Only one condition needs to match.
Output
| product_name | category |
|---|---|
| Ergonomic Chair | Furniture |
| USB-C Cable | Cables |
12. Case 3 — NOT for Exclusion
Query
SELECT product_name, category
FROM products
WHERE NOT category = 'Electronics';Explanation
Exclude Electronics.
Return everything else.
Output
| product_name | category |
|---|---|
| Gaming Mouse | Accessories |
| Mechanical Keyboard | Accessories |
| Ergonomic Chair | Furniture |
| USB-C Cable | Cables |
13. The Logic Trap — Mixing AND and OR
When AND and OR are used together, order matters.
This is one of the most common beginner mistakes.
SQL usually evaluates:
ANDOR
So AND is processed before OR.
This can produce unexpected results.
14. Use Parentheses to Control Logic
To avoid confusion, always group conditions with parentheses.
Parentheses force SQL to evaluate grouped conditions first.
This makes your logic clear and safe.
Query
SELECT product_name, category, price
FROM products
WHERE (category = 'Accessories' OR category = 'Cables') AND price < 20;Explanation
Step 1: Find rows where category is:
- Accessories
- or Cables
Step 2: From those rows, keep only rows where price < 20
Output
| product_name | category | price |
|---|---|---|
| USB-C Cable | Cables | 15 |
Why Parentheses Matter
Without parentheses, SQL may evaluate the conditions differently and return wrong results.
Parentheses make your logic explicit.
15. Common Mistakes to Avoid
1. Forgetting to Repeat Column Names
Incorrect:
WHERE category = 'Asia' OR 'Europe'Correct:
WHERE category = 'Asia' OR category = 'Europe'Each condition must be complete.
2. Using AND Incorrectly on Same Column
Incorrect:
WHERE category = 'Asia' AND category = 'Europe'This returns zero rows.
A single row cannot be both values at once.
3. Forgetting Parentheses
When mixing AND and OR, always use parentheses.
Without them, SQL may interpret your logic differently.
4. Confusing AND vs OR
AND= strictOR= flexible
Choose carefully.
16. Summary
Logical operators help SQL combine and control multiple conditions.
They are essential for real-world filtering.
AND= all conditions must be trueOR= at least one condition must be trueNOT= reverse the condition
When mixing conditions, use parentheses to control logic.
Final Mental Model
Logical operators help SQL answer:
- Must all rules pass?
- Can any one rule pass?
- Should this condition be excluded?