TutorialsSQLAND, OR, NOT
🟢 Free Demo
SQL TutorialTopic 4 of 20

AND, OR, NOT

Combine multiple conditions in WHERE

✅ What You Will Learn

How AND requires all conditions to be true
How OR requires at least one condition to be true
How NOT reverses a condition
How to use IN() as a cleaner alternative to multiple ORs
How brackets control evaluation order
How to combine AND, OR, and NOT in one query

Real queries rarely filter on just one condition. AND, OR, and NOT let you combine multiple conditions in the WHERE clause to express complex filters.

AND requires all conditions to be true for a row to be included. OR requires at least one condition to be true. NOT reverses a condition — it includes rows where the condition is false.

Understanding how these combine is essential for writing queries that answer real business questions.

📋 The orders table used in examples

order_idcustomer_nameproductamountcity
1001Rahul SharmaLaptop45000Delhi
1002Priya VermaMobile Phone18000Noida
1003Amit KumarHeadphones3500Gurgaon
1004Sneha KapoorLaptop52000Delhi
1005Vikram SinghTablet28000Noida

Syntax

SQL SYNTAX
-- AND: both conditions must be true
WHERE condition1 AND condition2

-- OR: at least one condition must be true
WHERE condition1 OR condition2

-- NOT: condition must be false
WHERE NOT condition

Examples

Example 1AND — both conditions must match
SELECT customer_name, product, amount
FROM orders
WHERE product = 'Laptop'
  AND amount > 50000;
OUTPUT
customer_name | product | amount
--------------|---------|-------
Sneha Kapoor  | Laptop  | 52000
💡

Only Sneha Kapoor matches both conditions — Laptop AND amount above 50000.

Example 2OR — either condition can match
SELECT customer_name, product, amount
FROM orders
WHERE product = 'Laptop'
   OR product = 'Mobile Phone';
OUTPUT
customer_name | product      | amount
--------------|--------------|-------
Rahul Sharma  | Laptop       | 45000
Priya Verma   | Mobile Phone | 18000
Sneha Kapoor  | Laptop       | 52000
Example 3NOT — exclude matching rows
SELECT customer_name, product
FROM orders
WHERE NOT product = 'Laptop';
OUTPUT
customer_name | product
--------------|-------------
Priya Verma   | Mobile Phone
Amit Kumar    | Headphones
Example 4Combining AND with OR — use brackets
-- Find Laptop orders OR Mobile Phone orders above ₹15,000
SELECT customer_name, product, amount
FROM orders
WHERE (product = 'Laptop' OR product = 'Mobile Phone')
  AND amount > 15000;
💡

Brackets control the order of evaluation — just like in maths. Without brackets, AND is evaluated before OR, which changes the result.

Example 5IN — cleaner alternative to multiple ORs
-- Instead of: WHERE product = 'Laptop' OR product = 'Mobile Phone'
SELECT customer_name, product
FROM orders
WHERE product IN ('Laptop', 'Mobile Phone', 'Headphones');
💡

IN is shorthand for multiple OR conditions on the same column. Much cleaner to read.

📌 Key Points to Remember

  • AND is stricter — both conditions must be true
  • OR is looser — either condition being true is enough
  • Use brackets when mixing AND and OR to control evaluation order
  • IN() is a cleaner way to write multiple OR conditions on one column
  • NOT IN() excludes a list of values

🏢 Real-World Application

Combining conditions is where SQL becomes truly powerful. A CRM analyst at a telecom company might query: customers who are from Delhi AND have been inactive for more than 60 days AND have a premium plan — that is three AND conditions in one WHERE clause. A marketing analyst might look for customers from Mumbai OR Pune OR Bangalore for a regional campaign — three OR conditions. The IN() operator is particularly useful: WHERE state IN ('Delhi', 'UP', 'Haryana', 'Rajasthan') instead of four OR conditions. These patterns are used constantly in real business reporting.

⚠️ Common Mistakes to Avoid

WRONGMixing AND and OR without brackets
FIXAND has higher precedence than OR. WHERE product = 'A' OR product = 'B' AND amount > 10000 is evaluated as WHERE product = 'A' OR (product = 'B' AND amount > 10000). Use brackets to make intent explicit.
WRONGUsing NOT IN with NULL values in the list
FIXNOT IN returns no rows if any value in the list is NULL. Always ensure your IN/NOT IN lists do not contain NULL values.
WRONGWriting NOT condition incorrectly: WHERE amount NOT > 10000
FIXThe correct syntax is WHERE NOT amount > 10000 or simply WHERE amount <= 10000. NOT applies to the whole condition, not after the column name.
✏️Test Yourself

What does the AND operator do in SQL?

❓ Frequently Asked Questions

What is the difference between AND and OR in SQL?

AND requires all conditions to be true for a row to be included. OR requires at least one condition to be true. AND is stricter and returns fewer rows; OR is looser and returns more rows.

What does IN do in SQL?

IN checks if a column value matches any value in a given list. WHERE product IN ('Laptop', 'Mobile', 'Tablet') is equivalent to WHERE product = 'Laptop' OR product = 'Mobile' OR product = 'Tablet' — just cleaner.

What does NOT IN do in SQL?

NOT IN excludes rows where the column value matches any value in the list. WHERE product NOT IN ('Laptop', 'Mobile') returns all rows except Laptop and Mobile orders.

Why do I need brackets when combining AND and OR?

AND is evaluated before OR (higher operator precedence), which can produce unexpected results. Brackets override this and make your intended logic explicit. Always use brackets when mixing AND and OR.

What is BETWEEN in SQL and how does it work?

BETWEEN is a shorthand for a range condition. WHERE amount BETWEEN 10000 AND 50000 is equivalent to WHERE amount >= 10000 AND amount <= 50000. Both endpoints are inclusive.

✏️ Practice Exercise

Write a query to find all orders where the product is either Laptop or Headphones, AND the amount is greater than ₹10,000.

← PreviousWHERE ClauseNext →ORDER BY
🎓 Level Up Faster

Learn SQL with Live Trainer Guidance

These tutorials give you the theory. Our live SQL course at EVIKA Academy, Noida teaches you to apply SQL on real company datasets — with a trainer who uses it daily at MakeMyTrip.