Skip to content

Logical Operators

Combining SQL conditions with AND, OR, NOT, and parentheses.

Builds on The WHERE Clause and Comparison Operators

Stage 1intro6 min read

SQL has defined logical precedence: NOT, then AND, then OR. 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:

  • AND
  • OR
  • NOT

3. Types of Logical Operators

OperatorMeaning
ANDAll conditions must be true
ORAt least one condition must be true
NOTReverses / 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.

countrypopulationarearegion
India14000000003287000Asia
China12000000004287000Asia
Brazil2140000008516000South America
Australia257000007692000Oceania
USA3310000009834000North 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

countrypopulationarearegion
India14000000003287000Asia
China12000000004287000Asia

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

countrypopulationarearegion
India14000000003287000Asia
China12000000004287000Asia
Australia257000007692000Oceania

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

countrypopulationarearegion
India14000000003287000Asia
China12000000004287000Asia
Brazil2140000008516000South America
Australia257000007692000Oceania

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_idproduct_namecategorypricestock_quantity
101Gaming MouseAccessories50100
102Mechanical KeyboardAccessories12050
103Curved MonitorElectronics30020
104Ergonomic ChairFurniture25010
105USB-C CableCables15200

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_namecategoryprice
Mechanical KeyboardAccessories120

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_namecategory
Ergonomic ChairFurniture
USB-C CableCables

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_namecategory
Gaming MouseAccessories
Mechanical KeyboardAccessories
Ergonomic ChairFurniture
USB-C CableCables

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:

  1. AND
  2. OR

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_namecategoryprice
USB-C CableCables15

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 = strict
  • OR = 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 true
  • OR = at least one condition must be true
  • NOT = 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?

In this topic

Done with this topic?

How well do you know it?

PreviousComparison Operators