The SQL Logical Operators (SQL AND OR NOT operators):
SQL provides three logical operators to combine conditions inside a WHERE clause:
| Operator | What It Does | Returns rows when… |
|---|---|---|
AND |
Combines two conditions | Both conditions are TRUE |
OR |
Combines two conditions | At least one condition is TRUE |
NOT |
Reverses a condition | The condition is FALSE |
We will use this Employees table for all examples:
+----+-----------+------------+--------+-----------+
| ID | Name | Department | Salary | City |
+----+-----------+------------+--------+-----------+
| 1 | Priya K. | Sales | 55000 | Mumbai |
| 2 | John D. | Marketing | 62000 | London |
| 3 | Arun M. | IT | 78000 | Delhi |
| 4 | Emma W. | Sales | 51000 | Sydney |
| 5 | Ravi S. | IT | 72000 | Mumbai |
| 6 | Sara L. | HR | 58000 | New York |
+----+-----------+------------+--------+-----------+
The AND Operator
The AND operator returns rows where all conditions are true. If any one condition is false, the row is excluded.
SELECT column1, column2
FROM table_name
WHERE condition1 AND condition2;
Example — Employees in IT earning over 75,000:
SELECT Name, Department, Salary
FROM Employees
WHERE Department = 'IT'
AND Salary > 75000;
+-----------+------------+--------+ | Name | Department | Salary | +-----------+------------+--------+ | Arun M. | IT | 78000 | +-----------+------------+--------+
Ravi S. is in IT but earns 72,000 (not over 75,000), so he is excluded. Both conditions must be true.
Example — Employees in Mumbai earning over 54,000:
SELECT Name, Salary, City
FROM Employees
WHERE City = 'Mumbai'
AND Salary > 54000;
+-----------+--------+--------+ | Name | Salary | City | +-----------+--------+--------+ | Priya K. | 55000 | Mumbai | | Ravi S. | 72000 | Mumbai | +-----------+--------+--------+
The OR Operator
The OR operator returns rows where at least one condition is true. The row is included if either condition (or both) is true.
SELECT column1, column2
FROM table_name
WHERE condition1 OR condition2;
Example — Employees in Sales OR Marketing:
SELECT Name, Department
FROM Employees
WHERE Department = 'Sales'
OR Department = 'Marketing';
+-----------+------------+ | Name | Department | +-----------+------------+ | Priya K. | Sales | | John D. | Marketing | | Emma W. | Sales | +-----------+------------+
A shorter way to write this is with the SQL IN operator, which you will learn in Chapter 17.
Example — Salary below 52,000 OR above 70,000:
SELECT Name, Salary
FROM Employees
WHERE Salary < 52000
OR Salary > 70000;
+-----------+--------+ | Name | Salary | +-----------+--------+ | Arun M. | 78000 | | Emma W. | 51000 | | Ravi S. | 72000 | +-----------+--------+
You could also achieve the salary range filter using the SQL BETWEEN operator in Chapter 18.
The NOT Operator
The NOT operator reverses a condition. It returns rows where the condition is false.
SELECT column1, column2
FROM table_name
WHERE NOT condition;
Example — Everyone NOT in Sales:
SELECT Name, Department
FROM Employees
WHERE NOT Department = 'Sales';
+-----------+------------+ | Name | Department | +-----------+------------+ | John D. | Marketing | | Arun M. | IT | | Ravi S. | IT | | Sara L. | HR | +-----------+------------+
Example — NOT in Mumbai:
SELECT Name, City
FROM Employees
WHERE NOT City = 'Mumbai';
+-----------+-----------+ | Name | City | +-----------+-----------+ | John D. | London | | Arun M. | Delhi | | Emma W. | Sydney | | Sara L. | New York | +-----------+-----------+
Combining AND, OR, and NOT
You can combine all three operators in a single query. Use parentheses to control the order of evaluation — just like math.
⚠️ Operator Precedence
SQL evaluates AND before OR. Without parentheses, A OR B AND C means A OR (B AND C) — not (A OR B) AND C. Always use parentheses to make your intent clear and avoid unexpected results.
Example — IT employees OR anyone earning over 60,000:
SELECT Name, Department, Salary
FROM Employees
WHERE Department = 'IT'
OR Salary > 60000;
+-----------+------------+--------+ | Name | Department | Salary | +-----------+------------+--------+ | John D. | Marketing | 62000 | | Arun M. | IT | 78000 | | Ravi S. | IT | 72000 | +-----------+------------+--------+
Example — Sales employees in Mumbai who earn over 50,000:
SELECT Name, Department, City, Salary
FROM Employees
WHERE Department = 'Sales'
AND City = 'Mumbai'
AND Salary > 50000;
+-----------+------------+--------+--------+ | Name | Department | City | Salary | +-----------+------------+--------+--------+ | Priya K. | Sales | Mumbai | 55000 | +-----------+------------+--------+--------+
Example — NOT in IT AND NOT in HR (using parentheses):
SELECT Name, Department
FROM Employees
WHERE NOT (Department = 'IT' OR Department = 'HR');
+-----------+------------+ | Name | Department | +-----------+------------+ | Priya K. | Sales | | John D. | Marketing | | Emma W. | Sales | +-----------+------------+
💡 AND vs OR — Quick Summary
| Operator | Condition 1 | Condition 2 | Row Included? |
|---|---|---|---|
AND |
TRUE | TRUE | ✅ Yes |
AND |
TRUE | FALSE | ❌ No |
OR |
TRUE | FALSE | ✅ Yes |
OR |
FALSE | FALSE | ❌ No |
For a complete reference of logical operators, see the official MySQL logical operators documentation. To sort your filtered results, learn SQL ORDER BY in the next chapter.
If you work with data regularly, explore our ready-made Dashboard Templates on NextGenTemplates that use SQL-powered data for business reporting.
📺 Visit our YouTube channel @NeoTechNavigators for step-by-step video tutorials and dashboard demos on this topic.
🧪 Try It Yourself
Write a query that finds employees who work in Mumbai OR Delhi, AND earn more than 60,000.
SELECT Name, City, Salary
FROM Employees
WHERE (City = 'Mumbai' OR City = 'Delhi')
AND Salary > 60000;
Expected Output:
+-----------+-----------+--------+ | Name | City | Salary | +-----------+-----------+--------+ | Arun M. | Delhi | 78000 | | Ravi S. | Mumbai | 72000 | +-----------+-----------+--------+
Note the parentheses around the OR condition — without them, the query would give different results due to AND having higher precedence than OR.
📝 What You Learned in This Chapter
ANDrequires all conditions to be true for a row to be includedORrequires at least one condition to be trueNOTreverses a condition — returns rows where it is false- Use parentheses to control evaluation order when combining operators
- SQL evaluates
ANDbeforeOR— always use parentheses to be safe - You can chain multiple AND/OR conditions in a single WHERE clause
Frequently Asked Questions
AND requires all conditions to be true — it narrows your results. OR requires at least one condition to be true — it broadens your results. For example, WHERE Department = 'IT' AND Salary > 70000 returns only IT employees earning over 70K, while using OR would return all IT employees plus anyone earning over 70K.WHERE City = 'Mumbai' AND Department = 'IT' AND Salary > 60000. When mixing AND and OR, always use parentheses to make the logic clear.AND before OR (higher precedence). Without parentheses, A OR B AND C is interpreted as A OR (B AND C). This can return unexpected results. Parentheses force the order you intend, making your queries both correct and readable.NOT and <> can produce similar results for simple comparisons, but NOT is more versatile. NOT can negate any condition, including complex ones like NOT (A OR B) or NOT IN (...). Use <> for simple “not equal” and NOT when negating compound conditions.A AND B returns the same rows as B AND A. However, some databases may evaluate conditions left to right for performance. Placing the most restrictive condition first can sometimes speed up the query on large datasets.📖 Chapter 6 of 30 — SQL Tutorial on Neotech Navigators



