MySQL OR Operator is one of the Logical Operator. Generally, we use this operator in the WHERE Clause to apply multiple filters on the records returned by SELECT Statement. This OR operator returns the result as:
- 1, if all operands are not nulls, and any of the one operands is non zero.
- 0, if all the operands are not nulls, and all of them are zeros.
- NULL, If one of the operands is NULL, and remaining operands are zeros, otherwise 1.
Let us see how to use the MySQL Logical Operator OR in the WHERE clause to filter the data. To explain the OR operator, we are going to use the below-shown data.
MySQL OR Operator – Command prompt
In this OR example, we pass Ones, Zeros, and Null values with a different combination. This example helps you to understand the Truth table behind the OR Operator.
-- MySQL OR Operator Example SELECT 0 OR 0; SELECT 1 OR 1; SELECT 1 OR 0; SELECT 1 OR NULL; SELECT 0 OR NULL; SELECT NULL OR NULL;
MySQL OR Operator Example
The MySQL OR Operator test multiple conditions in WHERE Clause. If either one of the conditions is TRUE, then it displays the records.
-- MySQL OR Operator Example USE company; SELECT CustID, First_Name, Last_Name, Education, Profession, Yearly_Income, Sales FROM customers WHERE Education = 'High School' OR Profession = 'Developer';
OR Operator Example 2
You can also use MySQL OR operator between three or 4 conditions. Let us apply OR operator between three conditions
-- MySQL OR Operator Example USE company; SELECT CustID, First_Name, Last_Name, Education, Profession, Yearly_Income, Sales FROM customers WHERE Education = 'High School' OR Profession = 'Developer' OR Yearly_Income > 90000;
The above statement retrieves Customers from Customers table whose Education is High School, or Profession as Developer, or Yearly Income is greater than 90000.