MariaDB Logical Operators: AND, OR, XOR, and NOT
In MariaDB, use AND when both conditions must be true, and OR when at least one must be true. Use XOR when exactly one of two conditions should be true, and NOT to reverse a condition. A NULL value can change the result, so this walkthrough displays each result before using WHERE to filter rows.
Set up the sample data in one connection
Select a practice database and run the following SQL. c1 is an integer and c2 holds one character. The examples reuse c1 = 1 and c2 = 'A' and include rows in which one or both values are missing.
CREATE TEMPORARY TABLE logic_demo (
id INT PRIMARY KEY,
c1 INT,
c2 CHAR(1)
);
INSERT INTO logic_demo (id, c1, c2) VALUES
(1, 1, 'A'), (2, 2, 'B'), (3, 3, 'C'), (4, 0, NULL),
(5, NULL, 'A'), (6, 1, 'B'), (7, 2, 'C'), (8, 3, NULL),
(9, 1, 'C'), (10, NULL, NULL);
The temporary table is visible only in the connection that created it. Run the rest of the examples in that connection. Open a new one to start over. Your account needs permission to create temporary tables.
Combine conditions with AND and OR
AND returns 1 when both comparisons are true. If one is false, the result is 0 even if the other is NULL. OR returns 1 when either comparison is true, even if the other is NULL.
SELECT id, c1, c2,
(c1 = 1 AND c2 = 'A') AS both_true,
(c1 = 1 OR c2 = 'A') AS either_true
FROM logic_demo
ORDER BY id;
SELECT id
FROM logic_demo
WHERE c1 = 1 OR c2 = 'A'
ORDER BY id;
In the first query, only ID 1 has 1 in both_true. IDs 1, 5, 6, 9 have 1 in either_true, and the second query returns exactly those four IDs. For ID 4, 0 OR NULL gives NULL in the displayed column, so the row is omitted by WHERE.
Apply XOR and NOT
With two conditions, XOR returns 1 if exactly one is true and 0 if both are true or both are false. A NULL operand makes the result NULL. NOT works on one condition, turning 1 into 0 and 0 into 1, while NULL stays NULL.
SELECT id, c1, c2,
(c1 = 1 XOR c2 = 'A') AS exactly_one,
NOT (c1 = 1) AS not_one
FROM logic_demo
ORDER BY id;
ID 1 satisfies both comparisons, so its exactly_one is 0. IDs 6, 9 satisfy only c1 = 1, so their result is 1. The not_one column is 1 for IDs 2, 3, 4, 7, 8 and NULL for IDs 5, 10, where c1 is missing.
Decide whether to include rows with NULL
NULL represents a missing or unknown value. An ordinary comparison with it produces NULL, but a logical operator may still determine the result from the other condition. See the MariaDB reference on NULL.
| Expression | Result | Reason |
|---|---|---|
0 AND NULL |
0 |
One false operand makes the whole condition false. |
1 AND NULL |
NULL |
The other operand could be true or false. |
1 OR NULL |
1 |
One true operand makes the whole condition true. |
0 OR NULL |
NULL |
It is unknown whether the other operand is true. |
1 XOR NULL |
NULL |
It is unknown whether exactly one operand is true. |
NOT NULL |
NULL |
Negating an unknown condition leaves it unknown. |
WHERE keeps a row only when the final condition is true. If the business rule is “the value differs from 1 or is missing,” add OR c1 IS NULL to NOT (c1 = 1) explicitly.
SELECT id
FROM logic_demo
WHERE NOT (c1 = 1) OR c1 IS NULL
ORDER BY id;
The query returns IDs 2, 3, 4, 5, 7, 8, 10. IDs 5, 10 are included because of the IS NULL test. Use IS NOT NULL when the rule requires a present value.
Group conditions with parentheses
By default, MariaDB gives the logical operators precedence in the order NOT, AND, XOR, OR. Parentheses specify which expressions to evaluate as a group. The first query looks for either comparison to be true while c1 is present. The next two queries show what happens when the same conditions are grouped differently. See the operator precedence reference.
SELECT id
FROM logic_demo
WHERE (c1 = 1 OR c2 = 'A') AND c1 IS NOT NULL
ORDER BY id;
SELECT id
FROM logic_demo
WHERE (c1 = 1 OR c2 = 'A') AND id >= 6
ORDER BY id;
SELECT id
FROM logic_demo
WHERE c1 = 1 OR c2 = 'A' AND id >= 6
ORDER BY id;
The first query returns IDs 1, 6, 9. The second returns only 6, 9 because id >= 6 applies to both comparisons. In the third query, AND binds before OR, so id >= 6 applies only to c2 = 'A' and ID 1 is included.
Check operator symbols and SQL mode
&& is an alias for AND. With PIPES_AS_CONCAT enabled, || concatenates strings. By default ! has higher precedence than NOT, and enabling HIGH_NOT_PRECEDENCE gives them the same precedence.
SELECT @@SESSION.sql_mode;
Check the active session mode if an existing query uses symbols or gives an unexpected result. For new conditions, keywords such as AND, OR, and NOT with explicit parentheses make the intended logic clear. See the MariaDB SQL mode reference.



