Lesson 18 of 60 – AND / OR Operators
30%

AND / OR Operators in SQL

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.

Note: AND requires all specified conditions to be true, while OR requires at least one condition to be true.

1. What is AND?

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.

2. What is OR?

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.

3. Basic AND Syntax

SELECT column_name
FROM table_name
WHERE condition1
AND condition2;

4. Basic OR Syntax

SELECT column_name
FROM table_name
WHERE condition1
OR condition2;

5. AND with Two Conditions

SELECT *
FROM Students
WHERE age > 18
AND age < 25;

This returns students whose age is greater than 18 and less than 25.

6. AND with Text Conditions

SELECT *
FROM Students
WHERE city = 'Patna'
AND course = 'Python';

This returns students who are from Patna and are enrolled in Python.

7. AND with Numeric Conditions

SELECT *
FROM Students
WHERE age >= 18
AND fee > 5000;

Both conditions must be satisfied.

8. AND with Three Conditions

SELECT *
FROM Students
WHERE age >= 18
AND course = 'Python'
AND city = 'Patna';

All three conditions must be true.

9. OR with Two Conditions

SELECT *
FROM Students
WHERE course = 'Python'
OR course = 'Java';

This returns students enrolled in either Python or Java.

10. OR with Three Conditions

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.

11. AND vs OR

Operator Meaning
AND All conditions must be true.
OR At least one condition must be true.

12. Understanding AND with Example

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.

13. Understanding OR with Example

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.

14. AND with WHERE

AND is frequently used together with WHERE.

SELECT name, course
FROM Students
WHERE age > 18
AND city = 'Patna';

15. OR with WHERE

SELECT name, course
FROM Students
WHERE city = 'Patna'
OR city = 'Delhi';

This returns students from either Patna or Delhi.

16. Combining AND and OR

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.

17. Using Parentheses

Parentheses allow you to group conditions.

SELECT *
FROM Students
WHERE age > 18
AND (course = 'Python' OR course = 'Java');

This means:

  • Age must be greater than 18.
  • Course must be either Python or Java.

18. AND with OR and Parentheses

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.

19. Multiple AND Conditions

SELECT *
FROM Students
WHERE age >= 18
AND fee > 5000
AND city = 'Patna'
AND course = 'Python';

Every condition must be satisfied.

20. Multiple OR Conditions

SELECT *
FROM Students
WHERE city = 'Patna'
OR city = 'Delhi'
OR city = 'Mumbai';

The record can match any one of the three cities.

21. AND with Comparison Operators

SELECT *
FROM Students
WHERE age >= 18
AND age <= 25;

This returns students whose age is from 18 through 25.

22. OR with Comparison Operators

SELECT *
FROM Students
WHERE age < 18
OR age > 60;

This returns records where age is either below 18 or above 60.

23. AND with LIKE

SELECT *
FROM Students
WHERE name LIKE 'A%'
AND course = 'Python';

This finds students whose names start with A and whose course is Python.

24. OR with LIKE

SELECT *
FROM Students
WHERE name LIKE 'A%'
OR name LIKE 'R%';

This finds students whose names start with A or R.

25. AND with IN

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.

26. OR with IN

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.

27. AND with BETWEEN

SELECT *
FROM Students
WHERE age BETWEEN 18 AND 25
AND course = 'Python';

This finds Python students whose age is between 18 and 25.

28. AND / OR with ORDER BY

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.

29. Practical Example

Suppose an institute wants students who:

  • Are at least 18 years old.
  • Are enrolled in Python or Java.
  • Have paid more than ₹5,000.

Query:

SELECT *
FROM Students
WHERE age >= 18
AND (course = 'Python' OR course = 'Java')
AND fee > 5000;

30. Complete AND / OR Example

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:

  • Finds students aged 18 or above.
  • Allows Python or Java courses.
  • Allows students from Patna or Delhi.
  • Requires a fee greater than 5000.
  • Sorts the result by fee in descending order.

📌 Key Points

  • AND requires all conditions to be true.
  • OR requires at least one condition to be true.
  • AND and OR are commonly used with the WHERE clause.
  • Multiple AND conditions can be used in one query.
  • Multiple OR conditions can be used in one query.
  • AND and OR can be combined in the same query.
  • Use parentheses to clearly group complex conditions.
  • AND can be combined with LIKE, IN, and BETWEEN.
  • OR can also be combined with LIKE, IN, and BETWEEN.
  • Parentheses make complex filtering logic easier to understand.

🧠 Quick Quiz

Question: Which SQL operator requires all specified conditions to be true?