Preloader

AND, OR, NOT Operators in SQL

PHP Tutorials
codevigyaan php

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

idnameagecitycourse
1Rahul20DelhiSQL
2Neha18MumbaiPython
3Amit22DelhiJava
4Priya17PuneSQL
5Karan21MumbaiSQL

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!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *