MariaDB Comparison Operators for Values, Ranges, and NULL

In MariaDB, = tests equality, <> and != test inequality, and > or < compare order. In SELECT, a comparison can produce 1 for true, 0 for false, or NULL for unknown. WHERE keeps just the true rows.

Set up the sample rows in one session

Select a practice database and run this setup with an account that can create temporary tables. Here, c_1 is an integer, c_2 is a string, and c_3 is a date. Run the examples in the same connection. The temporary table disappears when you disconnect.

CREATE TEMPORARY TABLE comparison_demo (
  id INT PRIMARY KEY,
  c_1 INT,
  c_2 VARCHAR(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
  c_3 DATE
);

INSERT INTO comparison_demo (id, c_1, c_2, c_3) VALUES
  (1, 1,    'A',  '2022-01-01'),
  (2, 2,    'B',  '2022-01-02'),
  (3, 3,    'C',  '2022-01-03'),
  (4, NULL, 'A',  '2022-01-04'),
  (5, NULL, NULL, '2022-01-05'),
  (6, 1,    NULL, NULL),
  (7, 2,    'B',  '2022-01-07'),
  (8, 3,    'C',  '2022-01-08');

Compare equality and order

Write integer literals such as 3 directly and quote strings such as 'A'. Give comparisons aliases in SELECT to see their result for each row. See the MariaDB comparison operator reference.

Operator Test
= Equal values
<> / != Different values
> / < Greater or less, excluding the boundary
>= / <= Greater or less, including the boundary
BETWEEN Inside a range, including both endpoints
IN Matches a value in the list
IS NULL The value is NULL
<=> Equality that treats two NULLs as equal
SELECT id, c_1 = 3 AS eq_3, c_2 = 'A' AS eq_a,
       c_2 <> 'A' AS ne_a, c_1 >= 2 AS ge_2, c_1 < 2 AS lt_2
FROM comparison_demo
ORDER BY id;

SELECT id FROM comparison_demo WHERE c_1 >= 2 ORDER BY id;

In the first query, IDs 3 and 8 have eq_3 equal to 1. The numeric comparisons are NULL in rows 4 and 5 because c_1 is NULL. The second query’s WHERE c_1 >= 2 returns only 2, 3, 7, and 8.

Filter by a range or a list

BETWEEN 2 AND 3 includes both endpoints. With matching types it is equivalent to c_1 >= 2 AND c_1 <= 3. Use NOT BETWEEN for values outside the range, IN for matches in a list, and NOT IN for values outside the list. See the BETWEEN reference.

SELECT id FROM comparison_demo WHERE c_1 BETWEEN 2 AND 3 ORDER BY id;
SELECT id FROM comparison_demo WHERE c_1 NOT BETWEEN 2 AND 3 ORDER BY id;
SELECT id FROM comparison_demo WHERE c_2 IN ('A', 'C') ORDER BY id;
SELECT id FROM comparison_demo WHERE c_2 NOT IN ('A', 'C') ORDER BY id;

SELECT 'B' NOT IN ('A', NULL) AS no_match,
       'A' NOT IN ('A', NULL) AS has_match;

The four ID lists are 2, 3, 7, 8, 1, 6, 1, 3, 4, 8, and 2, 7. In the final query, no_match is NULL and has_match is 0.

If an IN reference list contains NULL and no value matches, the result is unknown. For example, 'B' NOT IN ('A', NULL) cannot be true, so it does not pass WHERE. Check that the exclusion list contains only known values.

Test and compare NULL

= NULL and <> NULL cannot determine whether a value is missing. Use IS NULL or IS NOT NULL. ISNULL(c_1) also returns 1 for NULL. Use <=> to compare two values with two NULLs considered equal. See the MariaDB NULL reference.

SELECT id, c_1 = NULL AS eq_null, c_1 IS NULL AS is_null,
       c_1 IS NOT NULL AS has_value, c_1 <=> NULL AS safe_null,
       ISNULL(c_1) AS fn_is_null
FROM comparison_demo
ORDER BY id;

SELECT id FROM comparison_demo WHERE c_1 IS NULL ORDER BY id;
SELECT 2 = TRUE AS eq_true, 2 IS TRUE AS is_true,
       NULL IS NOT TRUE AS null_not_true;

In the first query, eq_null is NULL for every row, and is_null and safe_null equal 1 only for IDs 4 and 5. The last query gives 0, 1, 1. IS TRUE treats a nonzero number like 2 as true, while = TRUE compares it with 1. See the NULL comparison guide and WHERE conditions guide for further examples.

Account for string rules and date boundaries

String comparisons follow the column’s collation, which defines rules such as case sensitivity. Mixing strings and numbers can trigger implicit type conversion, so match the types of the values you compare. For a date range, use an inclusive start and an exclusive next boundary. This also avoids dropping times after midnight on the last included day if the column is later changed to a datetime.

SELECT id FROM comparison_demo WHERE c_2 >= 'B' ORDER BY id;
SELECT id FROM comparison_demo
WHERE c_3 >= DATE '2022-01-03' AND c_3 < DATE '2022-01-08'
ORDER BY id;

SELECT _utf8mb4'A' COLLATE utf8mb4_unicode_ci = _utf8mb4'a' AS case_insensitive,
       _utf8mb4'A' COLLATE utf8mb4_bin = _utf8mb4'a' AS case_sensitive;

The string query returns IDs 2, 3, 7, and 8. The date query returns 3, 4, 5, and 7, excluding the January 8 boundary. In the final query, 'A' equals 'a' under the case-insensitive collation but compares unequal under the binary collation. The results are 1 and 0.

Use related comparison functions

COALESCE() picks the first non-NULL value. GREATEST() and LEAST() return the highest and lowest argument. In the integer example below, both return NULL for a null input.

SELECT id, COALESCE(c_2, CAST(c_3 AS CHAR)) AS label,
       GREATEST(c_1, 2) AS higher_value, LEAST(c_1, 2) AS lower_value,
       INTERVAL(c_1, 2, 3) AS bucket
FROM comparison_demo
ORDER BY id;

ID 5 has 2022-01-05 as its label and ID 6 has NULL. The MariaDB INTERVAL() reference uses ascending integer thresholds to assign a bucket. Here, values below 2 map to 0, values from 2 up to 3 map to 1 with 3 excluded, values of 3 or more map to 2, and NULL maps to -1.