Course contentsShow
← Computer Science
Lesson 1565 of 2,87236. Database FundamentalsFree lesson

Logical Operators: AND, OR, NOT

Combining multiple conditions in WHERE clauses using logical operators to create complex filters.

Logical Operators: AND, OR, NOT

You'll learn how to combine multiple filtering conditions in SQL to create powerful, precise database queries.

What Are Logical Operators?

When filtering data with a WHERE clause, you often need to check multiple conditions at once. Logical operators let you combine simple conditions into complex filters. Think of them as "glue words" that connect your requirements, just like saying "I want a car that's red AND has four doors OR is a convertible, but NOT older than 2020."

The Three Core Operators

AND

Returns rows where both conditions are true. All criteria must be satisfied.

SELECT * FROM employees
WHERE department = 'Sales' AND salary > 50000;

This finds only salespeople earning over $50,000—both conditions must match.

OR

Returns rows where at least one condition is true. Any criterion being met is enough.

SELECT * FROM products
WHERE category = 'Electronics' OR category = 'Computers';

This retrieves items from either category.

NOT

Negates a condition, returning rows where the condition is false.

SELECT * FROM customers
WHERE NOT country = 'USA';

This finds all customers except those from the USA.

Combining Operators

You can mix operators for sophisticated filters. Use parentheses to control evaluation order:

SELECT * FROM orders
WHERE (status = 'Pending' OR status = 'Processing')
  AND NOT customer_id = 100;

This finds pending or processing orders, excluding those from customer 100.

Key Takeaway: Logical operators (AND, OR, NOT) let you build complex WHERE clause filters by combining multiple conditions, giving you precise control over which rows your query returns.