The AND and OR operators are used to combine multiple conditions in SQL queries. They are especially useful with the WHERE clause when you need to filter records using more than one condition.
The AND operator is used when all conditions in a query must be satisfied.
SELECT *
FROM Students
WHERE age > 18
AND course = 'Python';
A record must satisfy both conditions to be returned.
The OR operator is used when at least one condition must be true.
SELECT *
FROM Students
WHERE course = 'Python'
OR course = 'Java';
A student can match either Python or Java.
SELECT column_name
FROM table_name
WHERE condition1
AND condition2;
SELECT column_name
FROM table_name
WHERE condition1
OR condition2;
SELECT *
FROM Students
WHERE age > 18
AND age < 25;
This returns students whose age is greater than 18 and less than 25.
SELECT *
FROM Students
WHERE city = 'Patna'
AND course = 'Python';
This returns students who are from Patna and are enrolled in Python.
SELECT *
FROM Students
WHERE age >= 18
AND fee > 5000;
Both conditions must be satisfied.
SELECT *
FROM Students
WHERE age >= 18
AND course = 'Python'
AND city = 'Patna';
All three conditions must be true.
SELECT *
FROM Students
WHERE course = 'Python'
OR course = 'Java';
This returns students enrolled in either Python or Java.
SELECT *
FROM Students
WHERE course = 'Python'
OR course = 'Java'
OR course = 'PHP';
A record is returned if its course matches any one of these three values.
| Operator | Meaning |
|---|---|
| AND | All conditions must be true. |
| OR | At least one condition must be true. |
Suppose we have:
| Name | Age | Course |
|---|---|---|
| Rahul | 20 | Python |
| Priya | 22 | Java |
| Amit | 17 | Python |
Query:
SELECT *
FROM Students
WHERE age >= 18
AND course = 'Python';
Only Rahul satisfies both conditions.
Using the same data:
SELECT *
FROM Students
WHERE course = 'Python'
OR course = 'Java';
Rahul, Priya, and Amit can be returned because their course is either Python or Java.
AND is frequently used together with WHERE.
SELECT name, course
FROM Students
WHERE age > 18
AND city = 'Patna';
SELECT name, course
FROM Students
WHERE city = 'Patna'
OR city = 'Delhi';
This returns students from either Patna or Delhi.
AND and OR can be used in the same query.
SELECT *
FROM Students
WHERE age > 18
AND course = 'Python'
OR city = 'Patna';
For complex conditions, parentheses should be used to make the intended logic clear.
Parentheses allow you to group conditions.
SELECT *
FROM Students
WHERE age > 18
AND (course = 'Python' OR course = 'Java');
This means:
SELECT *
FROM Students
WHERE city = 'Patna'
AND (course = 'Python' OR course = 'Java');
This finds students from Patna who are enrolled in either Python or Java.
SELECT *
FROM Students
WHERE age >= 18
AND fee > 5000
AND city = 'Patna'
AND course = 'Python';
Every condition must be satisfied.
SELECT *
FROM Students
WHERE city = 'Patna'
OR city = 'Delhi'
OR city = 'Mumbai';
The record can match any one of the three cities.
SELECT *
FROM Students
WHERE age >= 18
AND age <= 25;
This returns students whose age is from 18 through 25.
SELECT *
FROM Students
WHERE age < 18
OR age > 60;
This returns records where age is either below 18 or above 60.
SELECT *
FROM Students
WHERE name LIKE 'A%'
AND course = 'Python';
This finds students whose names start with A and whose course is Python.
SELECT *
FROM Students
WHERE name LIKE 'A%'
OR name LIKE 'R%';
This finds students whose names start with A or R.
SELECT *
FROM Students
WHERE course IN ('Python', 'Java')
AND age >= 18;
This finds students who are enrolled in Python or Java and are at least 18 years old.
SELECT *
FROM Students
WHERE city IN ('Patna', 'Delhi')
OR course = 'Python';
A student is returned if they are from Patna or Delhi, or if their course is Python.
SELECT *
FROM Students
WHERE age BETWEEN 18 AND 25
AND course = 'Python';
This finds Python students whose age is between 18 and 25.
SELECT name, course, fee
FROM Students
WHERE age >= 18
AND (course = 'Python' OR course = 'Java')
ORDER BY fee DESC;
The filtered results are sorted by fee from highest to lowest.
Suppose an institute wants students who:
Query:
SELECT *
FROM Students
WHERE age >= 18
AND (course = 'Python' OR course = 'Java')
AND fee > 5000;
SELECT name, age, course, city, fee
FROM Students
WHERE age >= 18
AND (course = 'Python' OR course = 'Java')
AND (city = 'Patna' OR city = 'Delhi')
AND fee > 5000
ORDER BY fee DESC;
This query:
Question: Which SQL operator requires all specified conditions to be true?