Table of Contents
- Introduction
- What Are Logical Operators in SQL?
- The Role of Logical Operators in Filtering
- Basic Syntax and Usage
- Using
AND
to Combine Conditions - Using
OR
to Widen Conditions - Using
NOT
to Invert Conditions - Combining
AND
andOR
- Operator Precedence:
NOT
,AND
,OR
- Controlling Logic with Parentheses
- Real-World Use Case: Employee Filtering
- Real-World Use Case: Product Search
- Using Logical Operators in
UPDATE
- Using Logical Operators in
DELETE
- Logical Operators with
NULL
Checks - Performance Considerations
- Common Mistakes to Avoid
- Best Practices
- Summary Table of Logical Use Cases
- Summary and What’s Next
1. Introduction
SQL logical operators — AND
, OR
, and NOT
— are essential for writing powerful and flexible filtering conditions. They allow you to combine multiple expressions in your WHERE
clause to get precise control over query results.
2. What Are Logical Operators in SQL?
Logical operators are used to combine or negate conditions in a WHERE
clause. You can filter data using multiple rules instead of just one.
3. The Role of Logical Operators in Filtering
Instead of writing separate queries for each condition, logical operators let you write complex filters within a single SQL statement:
sqlCopyEditSELECT * FROM employees
WHERE department = 'IT' AND age > 30;
4. Basic Syntax and Usage
sqlCopyEditSELECT column1, column2
FROM table_name
WHERE condition1 [AND|OR|NOT] condition2;
Example:
sqlCopyEditSELECT * FROM users
WHERE city = 'Mumbai' AND is_active = TRUE;
5. Using AND
to Combine Conditions
The AND
operator returns true only if both conditions are true:
sqlCopyEditSELECT * FROM orders
WHERE status = 'shipped' AND payment_method = 'UPI';
Example Output Logic:
status | payment_method | Included |
---|---|---|
shipped | UPI | ✅ |
shipped | Credit Card | ❌ |
pending | UPI | ❌ |
6. Using OR
to Widen Conditions
The OR
operator returns true if either condition is true:
sqlCopyEditSELECT * FROM employees
WHERE department = 'HR' OR department = 'Admin';
You can match rows from either department.
7. Using NOT
to Invert Conditions
The NOT
operator negates the result of a condition:
sqlCopyEditSELECT * FROM users WHERE NOT city = 'Delhi';
This returns all users not from Delhi.
More examples:
sqlCopyEditSELECT * FROM orders WHERE NOT status = 'cancelled';
SELECT * FROM customers WHERE NOT age BETWEEN 18 AND 25;
8. Combining AND
and OR
You can use both AND
and OR
together for more advanced logic:
sqlCopyEditSELECT * FROM employees
WHERE (department = 'HR' OR department = 'Finance')
AND age > 30;
This selects employees from HR or Finance who are also older than 30.
9. Operator Precedence: NOT
, AND
, OR
SQL evaluates operators in the following order:
NOT
AND
OR
So this query:
sqlCopyEditSELECT * FROM users
WHERE NOT age < 18 AND city = 'Mumbai' OR is_active = TRUE;
Is interpreted as:
sqlCopyEdit((NOT age < 18) AND (city = 'Mumbai')) OR (is_active = TRUE)
Use parentheses to avoid ambiguity.
10. Controlling Logic with Parentheses
Parentheses clarify and control precedence:
sqlCopyEditSELECT * FROM orders
WHERE status = 'shipped' AND (payment_method = 'UPI' OR payment_method = 'Wallet');
Without parentheses, SQL may evaluate logic differently than intended.
11. Real-World Use Case: Employee Filtering
sqlCopyEditSELECT * FROM employees
WHERE department = 'IT'
AND (experience > 5 OR salary > 60000);
This returns IT employees who either have more than 5 years of experience or earn more than 60K.
12. Real-World Use Case: Product Search
sqlCopyEditSELECT * FROM products
WHERE (category = 'Books' OR category = 'Stationery')
AND price < 500;
Use logical operators to create advanced filters on e-commerce search or dashboards.
13. Using Logical Operators in UPDATE
sqlCopyEditUPDATE users
SET status = 'inactive'
WHERE last_login < '2024-01-01' AND is_active = TRUE;
You can update based on multiple logical checks.
14. Using Logical Operators in DELETE
sqlCopyEditDELETE FROM logs
WHERE severity = 'low' OR created_at < '2023-01-01';
This removes either old or low-priority logs.
15. Logical Operators with NULL
Checks
sqlCopyEditSELECT * FROM users
WHERE phone IS NULL OR email IS NULL;
You can combine logical operators with IS NULL
, IS NOT NULL
.
16. Performance Considerations
- Use indexed columns in conditions to speed up filtering.
- Avoid overly complex conditions in huge datasets without proper indexing.
- Group conditions for efficient evaluation.
17. Common Mistakes to Avoid
- Forgetting parentheses when combining
AND
andOR
- Incorrect assumptions about precedence
- Using
=
for NULL comparisons (instead ofIS NULL
) - Neglecting quotes around string literals
18. Best Practices
- Always group complex expressions with parentheses
- Test each condition individually before combining
- Prioritize readability — future you will thank you
- Use logical operators to reduce the number of queries
19. Summary Table of Logical Use Cases
Operator | Use Case | Example |
---|---|---|
AND | All conditions must be true | WHERE salary > 50000 AND city = 'Delhi' |
OR | At least one condition must be true | WHERE city = 'Delhi' OR city = 'Mumbai' |
NOT | Invert a condition | WHERE NOT is_active = TRUE |
Combined | Advanced conditions | WHERE (role = 'admin' OR role = 'editor') AND active |
20. Summary and What’s Next
Logical operators are indispensable in SQL for writing meaningful filters. They make your WHERE
clauses far more powerful by allowing multiple conditions and combinations.