w3resource

MySQL OR operator

OR operator

MySQL OR operator compares two expressions and returns TRUE if either of the expressions is TRUE.

This function is useful in -

  • It allows you to combine multiple conditions in a query, specifying that at least one of the conditions must be true for a record to be included in the result set.
  • OR makes queries more explicit. It clearly states that you want records that meet one condition or another, which can help prevent misunderstandings or misinterpretations of your query.
  • When you want to filter records based on multiple criteria, OR is crucial.
  • By using OR judiciously, you can optimize your queries to efficiently retrieve the desired data.
  • OR provides a way to logically combine conditions, allowing you to express complex logic in your queries.
  • In some scenarios, OR can be used to conditionally insert or update records based on the presence of specific conditions.
  • When you have complex conditions involving multiple logical operators like AND and OR, OR can be crucial for achieving the desired outcome.

Syntax:

OR, ||

When more than one logical operator is used in a statement, OR operators perform after AND operator. The order of evaluation can be changed by using parentheses.

The operator returns 1 when both operands are a non-NULL and one of them is nonzero and returns 0 when both operands are non-NULL and one of them is zero and returns NULL when one operand is NULL and other is zero and return 1 also when one is NULL and another operand is nonzero and NULL also when both operands and NULL.

MySQL Version: 8.0

Example: MySQL OR operator

The following MySQL statement satisfies the condition - "both operands are a non-NULL and one of them is nonzero", so it returns 1.

Code:

SELECT 5 || 5;

Output:

MySQL> SELECT 5 || 5;
+--------+
| 5 || 5 |
+--------+
|      1 | 
+--------+
1 row in set (0.00 sec)

Example of MySQL OR operator with at least one (zero) 0

The following MySQL statement satisfies the condition - "both operands are non-NULL and one of them is zero", so it returns 1.

Code:

SELECT 5 || 0;

Output:

MySQL> SELECT 5 || 0;
+--------+
| 5 || 0 |
+--------+
|      1 | 
+--------+
1 row in set (0.00 sec)

Example of MySQL OR operator when both operands are zero(0)

The following MySQL statement both of the operands are 0, so it returns 0.

Code:

SELECT 0 || 0;

Output:

MySQL> SELECT 0 || 0;
+--------+
| 0 || 0 |
+--------+
|      0 | 
+--------+
1 row in set (0.00 sec)

Example of MySQL OR operator with at least one NULL operand

The following MySQL statement satisfies the condition - "one operand is NULL and other is zero", so it returns NULL.

SELECT 0 || NULL;

Output:

MySQL> SELECT 0 || NULL;
+-----------+
| 0 || NULL |
+-----------+
|      NULL | 
+-----------+
1 row in set (0.01 sec)

Example of MySQL OR operator with NULL and non-zero operand

The following MySQL statement satisfies the condition - "one operand is NULL and other is non-zero", so it returns 1.

Code:

SELECT 5 || NULL;

Output:

MySQL> SELECT 5 || NULL;
+-----------+
| 5 || NULL |
+-----------+
|         1 | 
+-----------+
1 row in set (0.00 sec)

Example of MySQL OR operator with both NULL operands

The following MySQL statement both of the operands are NULL, so it returns NULL.

Code:

SELECT NULL || NULL;

Output:

MySQL> SELECT NULL || NULL;
+--------------+
| NULL || NULL |
+--------------+
|         NULL | 
+--------------+
1 row in set (0.00 sec)

Previous: NOT operator
Next: XOR operator



Follow us on Facebook and Twitter for latest update.