OR is a logical operator in Oracle that is used to combine two expressions and returns true if either one of the condition is true.
The syntax for this Logical condition is as follows:
expression1 OR expression2
It returns TRUE if either of the expressions are TRUE, it returns FALSE if both of them is FALSE. Otherwise it returns UNKNOWN.
To see the outcomes when we combine two expressions having TRUE, FALSE and NULL values, we can refer this table:
OR Logical operator is used after WHERE clause and can be used with SELECT, DELETE and UPDATE statements. It is also used while writing the join condition when we combine the tables.
AND, OR and NOT are the three logical operators used in Oracle.
Oracle OR Logical Condition Examples
We will be using Employees Table from HR schema for these examples:
1. Oracle OR to combine two expressions
Lets see one example of Logical OR operator using one query:
SELECT first_name, department_id, salary FROM employees WHERE department_id = 100 OR department_id= 60;
Result:
Using this query, we get all the employees who either belong to department=100 or 60.
2. Oracle OR to combine multiple expressions
We can also use Or logical operator to combine more than two conditions, lets check a query to understand this better:
SELECT first_name, department_id, salary FROM employees WHERE department_id= 50 OR department_id=60 OR department_id=70;
Lets expand the previous query to also include on other department.
Result:
This query will return all the employees who belong to either department=50, 60 or 70.
3. Oracle OR in combination with AND operator
Lets combine both logical operators OR and AND in a query to get specified output.
Suppose we want to find employees who either belong to department 50 or 60 and have salary less than 4500, then we use this query:
SELECT first_name, department_id, salary FROM employees WHERE (department_id = 50 OR department_id= 60) AND salary < 4500 ;
Result:
Using the combination of logical operators OR and AND we can retrieve the required data.
In this article we learned how to use logical operator OR in queries and to combine expressions.