đ 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
đ Basic Syntax
General Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1
OR condition2
OR condition3;đ Sample Table
Consider the following Students table:
| StudentID | Name | Department | Marks | City |
|---|---|---|---|---|
| 101 | Alice | Computer Science | 92 | Chennai |
| 102 | Bob | Mathematics | 85 | Coimbatore |
| 103 | Charlie | Physics | 78 | Madurai |
| 104 | David | Computer Science | 95 | Chennai |
| 105 | Eva | Physics | 88 | Coimbatore |
đ¯ 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.
| Operator | Example |
|---|---|
| = | 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
đ 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
đ AND vs OR
| Feature | AND | OR |
|---|---|---|
| Condition Requirement | All conditions must be true. | At least one condition must be true. |
| Result Set | More restrictive. | Less restrictive. |
| Typical Usage | Narrow 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
đ 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.