AND, OR, NOT Operators – SQL Tutorial for Beginners

11/22/2025
All Articles

SQL AND OR NOT operators tutorial with examples

AND, OR, NOT Operators – SQL Tutorial for Beginners

AND, OR, NOT Operators – SQL Tutorial for Beginners

SQL logical operators AND, OR, and NOT are used to filter data based on multiple conditions. These operators make SQL queries more powerful and flexible by allowing complex filtering logic.

In this beginner-friendly SQL tutorial, you’ll learn:

  • What AND, OR, NOT operators are

  • How to use each operator

  • Combined conditions

  • Real-world examples

  • Best practices


🔹 What Are Logical Operators in SQL?

Logical operators help you apply multiple conditions in a query. They are mainly used inside the WHERE clause.

The three most commonly used logical operators are:

  • AND – All conditions must be true

  • OR – At least one condition must be true

  • NOT – Negates a condition


1️⃣ AND Operator

The AND operator returns rows only if all conditions are true.

Syntax:

SELECT column1, column2
FROM table_name
WHERE condition1 AND condition2;

Example:

Retrieve employees from the IT department AND with a salary above 50,000.

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

2️⃣ OR Operator

The OR operator returns rows if any one of the conditions is true.

Syntax:

SELECT column1, column2
FROM table_name
WHERE condition1 OR condition2;

Example:

Retrieve employees who work in IT OR HR.

SELECT * FROM employees
WHERE department = 'IT'
OR department = 'HR';

3️⃣ NOT Operator

The NOT operator negates a condition.

Syntax:

SELECT column1, column2
FROM table_name
WHERE NOT condition;

Example:

Retrieve employees not in the Sales department.

SELECT * FROM employees
WHERE NOT department = 'Sales';

🔸 Combining AND, OR, NOT

You can combine operators to create complex logic.

Example 1: AND + OR

Retrieve employees from IT or HR with salary above 60,000.

SELECT * FROM employees
WHERE (department = 'IT' OR department = 'HR')
AND salary > 60000;

Example 2: AND + NOT

Retrieve employees in IT but not seniors.

SELECT * FROM employees
WHERE department = 'IT'
AND NOT position = 'Senior';

Example 3: OR + NOT

Retrieve employees not in Finance or have salary above 70k.

SELECT * FROM employees
WHERE NOT department = 'Finance'
OR salary > 70000;

🔸 Real-World Examples

Example 1: Filtering Products

SELECT * FROM products
WHERE price < 500
AND (category = 'Mobile' OR category = 'Electronics');

Example 2: Finding Active Users

SELECT * FROM users
WHERE is_active = 1
AND NOT city = 'Delhi';

Example 3: Customer Search

SELECT * FROM customers
WHERE name LIKE 'A%'
OR name LIKE 'S%';

🔹 Best Practices for AND, OR, NOT

✔ Use parentheses () to avoid confusion in combined conditions
✔ Always write readable queries
✔ Avoid unnecessary NOT conditions
✔ Test conditions individually before combining


Summary

In this tutorial, you learned:

  • How AND, OR, and NOT operators work

  • How to use multiple conditions

  • Real-world examples of logical filtering

  • Best practices for clean and accurate SQL queries

These operators are essential for data filtering and are used in almost every SQL query.

Article