AND, OR, NOT Operators in SQL
AND, OR, and NOT operators in SQL are used with the WHERE clause to filter data using more than one condition. They help you get exactly the records you want from a table.
Sample Data Table: students
| id | name | age | city | course |
|---|---|---|---|---|
| 1 | Rahul | 20 | Delhi | SQL |
| 2 | Neha | 18 | Mumbai | Python |
| 3 | Amit | 22 | Delhi | Java |
| 4 | Priya | 17 | Pune | SQL |
| 5 | Karan | 21 | Mumbai | SQL |
All the queries below use this table.
AND Operator in SQL
The AND operator returns a row only when all conditions are true.
Example
SELECT *
FROM students
WHERE city = 'Delhi' AND age > 19;
Result Explanation
This query returns Rahul and Amit because they both live in Delhi and their age is above 19.
OR Operator in SQL
The OR operator returns a row when any one condition is true.
Example
SELECT *
FROM students
WHERE city = 'Delhi' OR city = 'Mumbai';
Result Explanation
This query returns students who live in Delhi or Mumbai.
NOT Operator in SQL
The NOT operator removes rows that match a condition.
Example
SELECT *
FROM students
WHERE NOT city = 'Delhi';
Result Explanation
This query returns students who do not live in Delhi.
Using AND, OR, and NOT Together
You can combine these operators for more accurate filtering.
Example
SELECT *
FROM students
WHERE (city = 'Delhi' OR city = 'Mumbai')
AND NOT age < 18;
Result Explanation
This query returns students from Delhi or Mumbai who are 18 years or older.
Why Logical Operators Matter
AND, OR, and NOT help you
apply multiple conditions
filter real world data
avoid unwanted records
write clean SQL queries
They are used in almost every SQL project.
Summary
AND checks all conditions.
OR checks any condition.
NOT excludes matching data.
Check out our resources!
- Bootstrap Templates: Explore our Bootstrap Projects section.
- Free E-Books: Download your Free E-Books here.


