AND, OR, NOT Operators – SQL Tutorial for Beginners
SQL AND OR NOT operators tutorial with examples
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
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
The AND operator returns rows only if all conditions are true.
SELECT column1, column2
FROM table_name
WHERE condition1 AND condition2;
Retrieve employees from the IT department AND with a salary above 50,000.
SELECT * FROM employees
WHERE department = 'IT'
AND salary > 50000;
The OR operator returns rows if any one of the conditions is true.
SELECT column1, column2
FROM table_name
WHERE condition1 OR condition2;
Retrieve employees who work in IT OR HR.
SELECT * FROM employees
WHERE department = 'IT'
OR department = 'HR';
The NOT operator negates a condition.
SELECT column1, column2
FROM table_name
WHERE NOT condition;
Retrieve employees not in the Sales department.
SELECT * FROM employees
WHERE NOT department = 'Sales';
You can combine operators to create complex logic.
Retrieve employees from IT or HR with salary above 60,000.
SELECT * FROM employees
WHERE (department = 'IT' OR department = 'HR')
AND salary > 60000;
Retrieve employees in IT but not seniors.
SELECT * FROM employees
WHERE department = 'IT'
AND NOT position = 'Senior';
Retrieve employees not in Finance or have salary above 70k.
SELECT * FROM employees
WHERE NOT department = 'Finance'
OR salary > 70000;
SELECT * FROM products
WHERE price < 500
AND (category = 'Mobile' OR category = 'Electronics');
SELECT * FROM users
WHERE is_active = 1
AND NOT city = 'Delhi';
SELECT * FROM customers
WHERE name LIKE 'A%'
OR name LIKE 'S%';
✔ Use parentheses () to avoid confusion in combined conditions
✔ Always write readable queries
✔ Avoid unnecessary NOT conditions
✔ Test conditions individually before combining
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.