SQL Basics
- What is SQL?
- Data Definition Language
- Data Manipulation Language
- Data Query Language
- Data Control Language
- Transaction Control Language
- Tables and Schemas
- Data Types
- Constraints
Querying and Filtering
🧠 Understanding SQL Logical Operators: AND
, OR
, and NOT
Explained with Examples
When working with SQL to retrieve or manipulate data, you’ll often need to combine multiple conditions in your queries. That’s where logical operators come in.
In SQL, the logical operators AND
, OR
, and NOT
allow you to construct complex conditions in a readable and efficient way. They work hand-in-hand with the WHERE
clause to help you fetch the data that matches all, some, or none of the conditions.
In this guide, we’ll break down each of these operators with clear examples, diagrams, and use cases — all explained in simple, beginner-friendly terms.
🔍 What Are Logical Operators in SQL?
Logical operators allow you to combine two or more conditions in a SQL statement. Each condition results in a TRUE, FALSE, or UNKNOWN (if NULL
is involved), and logical operators help define how these results are evaluated together.
There are three main logical operators:
Operator | Description |
---|---|
AND | Returns true if both conditions are true |
OR | Returns true if any one condition is true |
NOT | Reverses the result of a condition |
These are used primarily with the WHERE
clause but also appear in JOIN
, HAVING
, and even CASE
statements.
🔗 AND
Operator in SQL
The AND
operator is used when you want multiple conditions to be true at the same time. If even one condition is false, the result will be false.
🧾 Syntax
SELECT * FROM table_nameWHERE condition1 AND condition2;
✅ Example
Let’s say we want to find employees who are in the “Sales” department and have a salary over $50,000.
SELECT *FROM employeesWHERE department = 'Sales' AND salary > 50000;
This will return only those employees who satisfy both conditions.
🧠 Real-World Use
In real applications, this might be used to filter users who are both active and have verified email addresses, like:
WHERE status = 'active' AND email_verified = TRUE;
🔄 OR
Operator in SQL
The OR
operator returns TRUE
if at least one of the conditions is true. It is helpful when you want results that satisfy either condition.
🧾 Syntax
SELECT * FROM table_nameWHERE condition1 OR condition2;
✅ Example
Find all customers who live in either “New York” or “California”.
SELECT *FROM customersWHERE state = 'New York' OR state = 'California';
This will return any customer from either of the two states.
🧠 Real-World Use
You might use this to get products that are either on sale or recently added:
WHERE on_sale = TRUE OR created_at > '2024-12-01';
🔀 Mixing AND
and OR
in a Query
You can combine AND
and OR
in one query, but be careful with operator precedence. Use parentheses to group conditions and make logic clear.
⚠️ Without Parentheses
SELECT *FROM employeesWHERE department = 'Sales' AND position = 'Manager' OR salary > 60000;
This might unexpectedly include employees with salary > 60000 even if they’re not in Sales.
✅ With Parentheses
SELECT *FROM employeesWHERE department = 'Sales' AND (position = 'Manager' OR salary > 60000);
This ensures the OR
applies only to Sales employees.
🚫 NOT
Operator in SQL
The NOT
operator is used to negate a condition. It reverses TRUE
to FALSE
and vice versa.
🧾 Syntax
SELECT * FROM table_nameWHERE NOT condition;
✅ Example
Get all users who are not active:
SELECT *FROM usersWHERE NOT status = 'active';
Or more cleanly:
WHERE status != 'active';
🧠 Real-World Use
Used to exclude a condition:
WHERE NOT (country = 'USA' OR country = 'Canada');
This returns users from countries other than USA and Canada.
🎓 Understanding Operator Precedence
When SQL evaluates a query, it uses a predefined order of operations:
NOT
AND
OR
This means NOT
is evaluated first, followed by AND
, and then OR
.
🧾 Example
SELECT *FROM employeesWHERE NOT department = 'HR' AND salary > 40000;
This will be interpreted as:
(NOT department = 'HR') AND (salary > 40000)
So always use parentheses to avoid confusion and ensure your logic is applied correctly.
🛠 Practical Example: Combining All Three Operators
Scenario: Find customers who are not from Texas, and either placed an order recently or have spent over $1,000.
SELECT *FROM customersWHERE NOT state = 'Texas' AND (last_order_date > '2024-01-01' OR total_spent > 1000);
This is a practical use case of combining NOT
, AND
, and OR
.
🧼 Tips for Beginners
- Use parentheses to make conditions clear.
- Think like the database: ask yourself which conditions must be true, false, or ignored.
- Test your logic on small datasets first.
- Remember: SQL is case-insensitive, but values (like
'active'
) are often case-sensitive depending on the DBMS.
💬 Common Mistakes to Avoid
Mistake | Correct Approach |
---|---|
Using = for NULL | Use IS NULL |
Forgetting parentheses | Always use them for clarity |
Misunderstanding precedence | Learn order: NOT > AND > OR |
Comparing text without quotes | Enclose string values in 'single quotes' |
SQL logical operators AND
, OR
, and NOT
may seem basic, but they are essential tools for writing powerful queries. These operators help you define exactly what data you want and ensure your filters work the way you intend.
By mastering logical operators, you take a huge step toward becoming proficient in SQL. Whether you’re writing reports, dashboards, or automation scripts — these operators will be your daily companions.