OR in SQL

🔀 The OR operator in SQL is a logical operator used to combine two or more conditions in a query. It returns records when at least one of the specified conditions is TRUE. The OR operator is commonly used with WHERE, HAVING, and other SQL clauses to retrieve records that match any of several conditions.

📖 What is the OR Operator?

The OR operator evaluates multiple conditions and returns a row if any one of the conditions is satisfied. If all conditions evaluate to FALSE, the row is excluded from the result set.

Information

Think of the OR operator as meaning "this condition or that condition (or both)".

📝 Basic Syntax

General Syntax

SELECT column1, column2, ...
FROM table_name
WHERE condition1
OR condition2
OR condition3;

📊 Sample Table

Consider the following Students table:

StudentIDNameDepartmentMarksCity
101AliceComputer Science92Chennai
102BobMathematics85Coimbatore
103CharliePhysics78Madurai
104DavidComputer Science95Chennai
105EvaPhysics88Coimbatore

đŸŽ¯ Using OR with Two Conditions

Retrieve students who belong to either the Computer Science department or the Physics department.

OR Example

SELECT *
FROM Students
WHERE Department = 'Computer Science'
OR Department = 'Physics';

The query returns students from either department because satisfying either condition is enough.

📌 Using OR with Multiple Conditions

You can combine more than two conditions using the OR operator.

Multiple OR Conditions

SELECT *
FROM Students
WHERE Department = 'Computer Science'
OR Department = 'Mathematics'
OR City = 'Madurai';

A row is returned if any of the conditions is true.

âš–ī¸ OR with Comparison Operators

The OR operator works with all comparison operators.

OperatorExample
= Department = 'Physics'
> Marks > 90
< Marks < 60
>= Marks >= 80
<= Age <= 21
<> City <> 'Chennai'

🔍 OR with LIKE

Combine pattern matching with multiple conditions.

LIKE with OR

SELECT *
FROM Students
WHERE Name LIKE 'A%'
OR Name LIKE 'D%';

📌 OR with IN

Multiple OR conditions checking the same column can often be replaced with the IN operator.

Using OR

SELECT *
FROM Students
WHERE Department = 'Computer Science'
OR Department = 'Physics'
OR Department = 'Mathematics';

Equivalent Using IN

SELECT *
FROM Students
WHERE Department IN (
    'Computer Science',
    'Physics',
    'Mathematics'
);

Tip

When checking multiple values for the same column, IN is usually shorter and easier to read than multiple OR conditions.

📊 OR with ORDER BY

After filtering rows with OR, the results can be sorted using ORDER BY.

Filter and Sort

SELECT Name,
       Department,
       Marks
FROM Students
WHERE Department = 'Physics'
OR Department = 'Computer Science'
ORDER BY Marks DESC;

🧮 OR with HAVING

The OR operator can also be used in the HAVING clause to filter grouped results.

HAVING with OR

SELECT Department,
       COUNT(*) AS TotalStudents,
       AVG(Marks) AS AverageMarks
FROM Students
GROUP BY Department
HAVING COUNT(*) >= 2
OR AVG(Marks) >= 90;

âš ī¸ Combining AND and OR

When AND and OR appear in the same query, use parentheses to clearly define the intended logic.

Using Parentheses

SELECT *
FROM Students
WHERE (Department = 'Computer Science'
       OR Department = 'Physics')
AND Marks >= 90;

Important

SQL evaluates AND before OR. Parentheses remove ambiguity and make queries easier to understand.

📊 AND vs OR

FeatureANDOR
Condition RequirementAll conditions must be true.At least one condition must be true.
Result SetMore restrictive.Less restrictive.
Typical UsageNarrow filtering.Alternative filtering.

đŸ’ŧ Real-World Example

A university wants to display students who either belong to the Computer Science department or have scored at least 90 marks.

Real-World Query

SELECT StudentID,
       Name,
       Department,
       Marks
FROM Students
WHERE Department = 'Computer Science'
OR Marks >= 90
ORDER BY Marks DESC;

This query returns students who satisfy either of the specified conditions.

âš ī¸ Best Practices

Best Practice

Use OR when records can satisfy any one of several conditions. Replace multiple equality checks on the same column with IN where appropriate, use parentheses when combining AND and OR, and format each condition on a separate line to improve readability.

🚀 Key Points to Remember

  • 📌 OR returns rows when at least one condition is true.
  • 📌 It is commonly used with WHERE and HAVING.
  • 📌 It works with comparison operators, LIKE, BETWEEN, and other SQL operators.
  • 📌 Use IN instead of multiple OR conditions on the same column when appropriate.
  • 📌 Use parentheses when mixing AND and OR in a query.
>>"The OR operator broadens your search by returning records that satisfy any matching condition."

Summary

✅ The OR operator is a fundamental SQL logical operator that retrieves records when one or more conditions are true. By combining it with comparison operators, pattern matching, grouping, and sorting, you can build flexible and powerful SQL queries to retrieve exactly the data you need.