🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
NEWSLIVESearch All Content
đŸ’ŧ Servicesâ„šī¸ Aboutâœ‰ī¸ ContactView Pricing Plansfrom $10

AND, OR, NOT Operators

SQL FundamentalsDMLđŸŸĸ Free Lesson
SQL Fundamentals

AND, OR, NOT Operators

Combine multiple conditions in your WHERE clause with AND, OR, and NOT.

  • AND — All conditions must be true
  • OR — At least one condition must be true
  • NOT — Negates a condition Master Boolean logic to build precise filters.

What Are Logical Operators?

AND (Intersection)ABA∊BBoth must be TRUEOR (Union)ABAâˆĒBAt least one TRUENOT (Complement)NOT ANegates condition
SELECT * FROM employees
WHERE department = 'Engineering' AND salary > 70000;

AND Operator

AND returns TRUE only when all conditions are TRUE. Use it to narrow results.

-- Find active products under $500
SELECT * FROM products
WHERE category = 'Electronics'
AND price < 500
AND stock_quantity > 0;
-- Date range with AND
SELECT * FROM orders
WHERE order_date >= '2024-01-01'
AND order_date <= '2024-12-31'
AND status != 'Cancelled';
Condition ACondition BA AND B
TRUETRUETRUE
TRUEFALSEFALSE
FALSETRUEFALSE
FALSEFALSEFALSE

OR Operator

OR returns TRUE when any condition is TRUE. Use it to broaden results.

-- Find employees in Engineering or Sales
SELECT * FROM employees
WHERE department = 'Engineering'
OR department = 'Sales';
-- Multiple OR conditions
SELECT * FROM customers
WHERE city = 'New York'
OR city = 'Los Angeles'
OR city = 'Chicago';
Condition ACondition BA OR B
TRUETRUETRUE
TRUEFALSETRUE
FALSETRUETRUE
FALSEFALSEFALSE

NOT Operator

NOT negates a condition, reversing its Boolean value.

-- Exclude Engineering department
SELECT * FROM employees
WHERE NOT department = 'Engineering';
-- NOT with IN
SELECT * FROM products
WHERE NOT category_id IN (1, 2, 3);
-- NOT with EXISTS
SELECT * FROM customers c
WHERE NOT EXISTS (
    SELECT 1 FROM orders o
    WHERE o.customer_id = c.customer_id
);
ConditionNOT Condition
TRUEFALSE
FALSETRUE
UNKNOWNUNKNOWN

Operator Precedence

SQL evaluates operators in a specific order. Understanding precedence prevents unexpected results.

-- Precedence: NOT > AND > OR
-- Without parentheses (AND binds first)
SELECT * FROM employees
WHERE department = 'Engineering'
OR department = 'Sales'
AND salary > 80000;
-- Interpreted as: Engineering OR (Sales AND salary > 80000)

-- With parentheses (clear intent)
SELECT * FROM employees
WHERE (department = 'Engineering' OR department = 'Sales')
AND salary > 80000;
-- Interpreted as: (Engineering OR Sales) AND salary > 80000
-- Complex expression with clear precedence
SELECT * FROM products
WHERE (category = 'Electronics' OR category = 'Computers')
AND (price < 500 OR price > 2000)
AND NOT status = 'Discontinued';

Combining All Three

Real-world queries often mix AND, OR, and NOT for precise filtering.

-- Find active customers in specific cities who have not placed orders
SELECT * FROM customers
WHERE (city = 'New York' OR city = 'Los Angeles')
AND status = 'Active'
AND NOT customer_id IN (
    SELECT DISTINCT customer_id FROM orders
);
-- Find products that are in stock but not in clearance
SELECT * FROM products
WHERE stock_quantity > 0
AND NOT status = 'Discontinued'
AND (category = 'Electronics' OR category = 'Clothing');

Performance Considerations

-- Slow: OR may bypass index
SELECT * FROM employees
WHERE department = 'Engineering'
OR department = 'Sales';

-- Faster: UNION with indexed columns
SELECT * FROM employees WHERE department = 'Engineering'
UNION
SELECT * FROM employees WHERE department = 'Sales';
OperatorIndex FriendlyAlternative
ANDYes—
ORSometimesUNION
NOT INSometimesNOT EXISTS, LEFT JOIN
NOT EXISTSYes—

Practice Exercises

Exercise 1: Find all active Electronics products under $500 with stock.

-- Solution
SELECT * FROM products
WHERE category = 'Electronics'
AND price < 500
AND stock_quantity > 0
AND status = 'Active';

Exercise 2: Find employees who are managers or earn more than $100,000.

-- Solution
SELECT * FROM employees
WHERE job_title = 'Manager'
OR salary > 100000;

Exercise 3: Find customers not from New York who have placed at least one order.

-- Solution
SELECT * FROM customers c
WHERE NOT city = 'New York'
AND EXISTS (
    SELECT 1 FROM orders o
    WHERE o.customer_id = c.customer_id
);

Summary

OperatorLogicUse Case
ANDAll TRUENarrow results
ORAny TRUEBroaden results
NOTNegateExclude rows
INValue in listMultiple OR conditions
BETWEENRange checkInclusive range

Extended Learning: Beyond the Basics

Key Concepts Summary

This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.

Prerequisite Knowledge

Before studying this topic in depth, ensure you understand:

  • Basic definitions and terminology
  • Core formulas and their derivations
  • Common applications in real-world scenarios
  • Typical pitfalls and misconceptions

Step-by-Step Walkthrough

Let us work through a complete example to solidify understanding:

Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.

Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.

Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.

Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.

Common Mistakes to Avoid

  1. Misidentifying the problem type — Always start by categorizing the problem
  2. Incorrect formula application — Verify the formula applies to your specific case
  3. Unit errors — Always check units are consistent throughout
  4. Sign errors — Pay careful attention to positive and negative values
  5. Rounding too early — Keep full precision until the final answer
  6. Forgetting constraints — Check that solutions satisfy all given conditions

Real-World Applications

Understanding this concept is essential for:

  • Advanced problem solving in examinations
  • Building intuition for more complex topics
  • Developing analytical thinking skills
  • Applying mathematical reasoning to practical situations

Practice Exercises

  1. Basic: Solve 5 problems of increasing difficulty using the core concept.
  2. Intermediate: Apply the concept to word problems with multiple steps.
  3. Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
  4. Challenge: Create your own problem that requires this concept plus additional knowledge.

Interview and Exam Tips

  • Common question patterns: Identify which exam or interview formats test this topic
  • Time management: Practice completing problems within time constraints
  • Common traps: Know the typical mistakes examiners set as distractors
  • Verification strategy: Develop a quick method to check your answer

Related Topics

Quick Reference Card

ConceptFormula/RuleKey InsightCommon Use
Core DefinitionSee main textUnderstand the intuitionFoundation for all applications
Key FormulaSee main textMemorize the derivationDirect problem solving
Special CaseSee main textRecognize when it appliesSimplifies complex problems
Edge CaseSee main textKnow the limitationsAvoids errors

Further Reading

For students who want to go deeper:

  • Standard textbooks on the subject
  • Research papers applying this concept
  • Online courses with video explanations
  • Practice problem collections with detailed solutions
CW

ChatWhole Learn Editorial Team

Written by experienced data scientists, software engineers, and educators. All content is reviewed by industry professionals to ensure accuracy and relevance.

Last reviewed: 2026-11-14

—
☆☆☆☆☆
0 ratings

Rate & Feedback

Need Expert SQL Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement