w3resource
MYSQL Tutorial

MySQL OR operator

OR operator

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

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: 5.6

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;

Sample 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;

Sample 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;

Sample 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;

Sample 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;

Sample 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;

Sample Output:

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

Previous: NOT operator
Next: XOR operator