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 operator returns the result as:
- 1, if all operands are non nulls, and any one operand is non zero.
- 0, if all the operands are not nulls, and all of them are zeros.
- NULL, If one of the operand is NULL, and remaining operands are zeros, otherwise 1.
In this article we will show you, How to use the MySQL Logical Operator OR in the WHERE Clause to filter the data. In order to explain the OR operator, we are going to use the below shown data.
MySQL OR Operator – Command prompt
In this example we will pass Ones, Zeros, and Null values with 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
In MySQL, OR Operator is used to test multiple conditions in WHERE Clause. If either one of the conditions is TRUE then it will display 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';
Above statement will retrieve all the Customers present in the Customers table whose Education is High School, or Profession as Developer.
MySQL OR Operator Example 2
You can also use 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;
Above statement will retrieve Customers from Customers table whose whose Education is High School, or Profession as Developer, or Yearly Income is greater than 90000.
Thank You for Visiting Our Blog