w3resource

MySQL AND operator

AND operator

MySQL logical AND operator compares two expressions and returns true if both of the expressions are true.

Syntax:

AND, &&

This operator returns 1 if all operands are nonzero and not NULL, 0 if one or more operands are 0, otherwise, NULL is returned.

MySQL Version: 5.6

Example: MySQL AND operator

In the following MySQL statement all the operands are non-zero (5 and 5) and both of them are not NULL, so logical and operator 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 AND operator with zero input

In the following MySQL statement, one of the operands is non zero (5 and 0), so logical and operator returns 0.

Code:

SELECT 5 && 0;

Sample Output:

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

Example of MySQL AND operator with NULL input

In the following MySQL statement, one of the operands is non zero (5) and another one is NULL, so logical and operator returns NULL.

Code:

SELECT 5 && NULL;

Sample Output:

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

Example of MySQL AND operator with zero and NULL input

In the following MySQL statement, one of the operands is zero (0) and another one is NULL, so logical and operator returns 0.

Code:

SELECT 0 && NULL;

Sample Output:

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

Example of MySQL AND operator with both inputs are NULL

In the following MySQL statement, all the operands are NULL. So the operator returns NULL.

Code:

SELECT NULL && NULL;

Sample Output:

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

Previous: NOT LIKE
Next: NOT operator



Inviting useful, relevant, well-written and unique guest posts