The OR operator is used in a WHERE clause to combine two or more conditions.
- A row is included in the result if any of the conditions are true.
- If all conditions are false, the row is excluded.
Basic syntax:
Step 1: Example table
Assume the Employees table:
| EmployeeID | FirstName | LastName | Department | Salary |
|---|---|---|---|---|
| 1 | John | Doe | IT | 5000 |
| 2 | Jane | Smith | HR | 4500 |
| 3 | Mike | Brown | IT | 6000 |
| 4 | Sara | White | HR | 4700 |
| 5 | Tom | Green | IT | 5200 |
Step 2: Using OR to filter rows
Example: Employees in IT department OR salary greater than 5000
Result:
| FirstName | Department | Salary |
|---|---|---|
| John | IT | 5000 |
| Mike | IT | 6000 |
| Tom | IT | 5200 |
Explanation:
- John → IT → included
- Mike → IT and Salary > 5000 → included
- Tom → IT and Salary > 5000 → included
- Jane and Sara → HR and Salary ≤ 5000 → excluded
Step 3: Using multiple OR conditions
Example: Employees in IT OR HR OR salary > 5000
Result:
| FirstName | Department | Salary |
|---|---|---|
| John | IT | 5000 |
| Jane | HR | 4500 |
| Mike | IT | 6000 |
| Sara | HR | 4700 |
| Tom | IT | 5200 |
Here, all employees match at least one condition.
Step 4: Combining AND and OR
When combining AND and OR, use parentheses to clarify logic.
Example: IT department AND (salary > 5000 OR EmployeeID = 1)
Result:
| FirstName | Department | Salary |
|---|---|---|
| John | IT | 5000 |
| Mike | IT | 6000 |
| Tom | IT | 5200 |
Parentheses ensure correct order of evaluation.
Step 5: Using IN as a shortcut for OR
Instead of multiple ORs, you can use IN:
Equivalent to:
WHERE Department = 'IT' OR Department = 'HR'
