SQL AND OR NOT operators
SQL

SQL AND, OR, NOT — How to Combine Multiple Conditions

CHAPTER 6 of 30 SQL Tutorial — Free Course on Neotech Navigators
In the previous chapter, you learned to filter rows with a single condition using WHERE. But real-world queries often need multiple conditions. The SQL AND, OR, NOT operators let you combine conditions to filter data with precision. In this chapter, you will master all three logical operators with practical examples.

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:

TABLE: Employees
+----+-----------+------------+--------+-----------+
| 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.

SQL — AND Syntax
SELECT column1, column2
FROM table_name
WHERE condition1 AND condition2;

Example — Employees in IT earning over 75,000:

SQL
SELECT Name, Department, Salary
FROM Employees
WHERE Department = 'IT'
  AND Salary > 75000;
✓ OUTPUT
+-----------+------------+--------+
| 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:

SQL
SELECT Name, Salary, City
FROM Employees
WHERE City = 'Mumbai'
  AND Salary > 54000;
✓ OUTPUT
+-----------+--------+--------+
| 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.

SQL — OR Syntax
SELECT column1, column2
FROM table_name
WHERE condition1 OR condition2;

Example — Employees in Sales OR Marketing:

SQL
SELECT Name, Department
FROM Employees
WHERE Department = 'Sales'
   OR Department = 'Marketing';
✓ OUTPUT
+-----------+------------+
| 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:

SQL
SELECT Name, Salary
FROM Employees
WHERE Salary < 52000
   OR Salary > 70000;
✓ OUTPUT
+-----------+--------+
| 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.

SQL — NOT Syntax
SELECT column1, column2
FROM table_name
WHERE NOT condition;

Example — Everyone NOT in Sales:

SQL
SELECT Name, Department
FROM Employees
WHERE NOT Department = 'Sales';
✓ OUTPUT
+-----------+------------+
| Name      | Department |
+-----------+------------+
| John D.   | Marketing  |
| Arun M.   | IT         |
| Ravi S.   | IT         |
| Sara L.   | HR         |
+-----------+------------+

Example — NOT in Mumbai:

SQL
SELECT Name, City
FROM Employees
WHERE NOT City = 'Mumbai';
✓ OUTPUT
+-----------+-----------+
| 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:

SQL
SELECT Name, Department, Salary
FROM Employees
WHERE Department = 'IT'
   OR Salary > 60000;
✓ OUTPUT
+-----------+------------+--------+
| 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:

SQL
SELECT Name, Department, City, Salary
FROM Employees
WHERE Department = 'Sales'
  AND City = 'Mumbai'
  AND Salary > 50000;
✓ OUTPUT
+-----------+------------+--------+--------+
| Name      | Department | City   | Salary |
+-----------+------------+--------+--------+
| Priya K.  | Sales      | Mumbai | 55000  |
+-----------+------------+--------+--------+

Example — NOT in IT AND NOT in HR (using parentheses):

SQL
SELECT Name, Department
FROM Employees
WHERE NOT (Department = 'IT' OR Department = 'HR');
✓ OUTPUT
+-----------+------------+
| 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.

ANSWER
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

  • AND requires all conditions to be true for a row to be included
  • OR requires at least one condition to be true
  • NOT reverses a condition — returns rows where it is false
  • Use parentheses to control evaluation order when combining operators
  • SQL evaluates AND before OR — always use parentheses to be safe
  • You can chain multiple AND/OR conditions in a single WHERE clause

Frequently Asked Questions

What is the difference between AND and OR in SQL?
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.
Can I use multiple AND or OR in one query?
Yes. You can chain as many AND and OR operators as needed. For example: WHERE City = 'Mumbai' AND Department = 'IT' AND Salary > 60000. When mixing AND and OR, always use parentheses to make the logic clear.
Why do I need parentheses with AND and OR?
SQL evaluates 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.
Is NOT the same as using != or <>?
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.
Does the order of conditions in AND or OR matter?
Logically, the order does not change the result — 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

PK
Meet PK, the founder of NeotechNavigators.com! With over 15 years of experience in Data Visualization, Excel Automation, and dashboard creation. PK is a Microsoft Certified Professional who has a passion for all things in Excel. PK loves to explore new and innovative ways to use Excel and is always eager to share his knowledge with others. With an eye for detail and a commitment to excellence, PK has become a go-to expert in the world of Excel. Whether you're looking to create stunning visualizations or streamline your workflow with automation, PK has the skills and expertise to help you succeed. Join the many satisfied clients who have benefited from PK's services and see how he can take your data analysis skills to the next level!
https://neotechnavigators.com