Comparison operators
=, <=>, <>, <, BETWEEN, IN, IS, row comparisons and ANY, SOME and ALL with subqueries: what each returns, and how text and NULL are compared.
A comparison gives 1 (true), 0 (false) or NULL (unknown).
Syntax#
expr { = | <=> | <> | != | < | <= | > | >= } expr
expr IS [NOT] { NULL | TRUE | FALSE | UNKNOWN }
expr [NOT] BETWEEN low AND high
expr [NOT] IN (value [, value] ... | subquery)
expr { = | <> | != | < | <= | > | >= } { ANY | SOME | ALL } (subquery)
(expr, expr [, ...]) { = | <=> | <> | < | <= | > | >= } (expr, expr [, ...])
(expr, expr [, ...]) [NOT] IN ( (value, value ...) [, ...] | subquery )
[NOT] EXISTS (subquery)Operators#
| Operator | Meaning |
|---|---|
= | equal; NULL if either side is NULL |
<=> | null-safe equal: 1 if both are NULL, 0 if only one is, otherwise as = |
<>, != | not equal |
<, <=, >, >= | order |
IS NULL, IS NOT NULL | the value is or is not NULL |
IS TRUE, IS FALSE, IS UNKNOWN | the truth of the value; always 0 or 1 (NOT negates) |
BETWEEN a AND b | x >= a AND x <= b; three-valued |
IN (list) | equal to a member of the list; NULL if there is no match and a member or the value is NULL |
IN (subquery) | equal to a row of the answer |
EXISTS (subquery) | the subquery returns a row |
= ANY, = SOME | the same as IN |
<> ALL | the same as NOT IN |
op ANY, op SOME | true if the comparison is true for at least one row of the answer; an empty answer is false |
op ALL | true if it is true for every row; an empty answer is true |
Remarks#
- Text compares without regard to case (see Collation and case sensitivity). A number compared with text is compared as numbers (the text is read as a number). Two texts that look like numbers are compared as texts:
'10' < '9'is 1. NULLgivesNULLfor all operators except<=>,ISandEXISTS; see NULL handling.- Row comparison.
(a, b) = (1, 2)is true if all pairs are equal,<>if one pair differs, and<,<=,>,>=are decided by the first pair that differs. The parser turns a row comparison into the comparisons of the parts. Rows of different sizes are error 1241 (The operand must have n column(s)); a row compared with a single value is the same error.(a, b) IN ((1, 2), (3, 4))and(a, b) IN (SELECT x, y ...)are read; so is(a, b) = (SELECT x, y ...). ANY,SOME,ALL. A subquery with one column is expected;x = ANY (subquery)andx <> ALL (subquery)becomeINandNOT IN; the others are the OR (AND) of the comparisons with each row of the answer, withNULLs as in three-valued logic.BETWEENcompares with the loose rules of>=and<=:'b' BETWEEN 'A' AND 'C'is 1.- Indexes. The simple conditions of a
WHERE(column op constant,IN,BETWEENjoined byAND) are given to Raft DB, which uses its indexes (hash for=andIN, ordered for ranges); the front-end still checks the whole condition. - Not supported yet:
LIKE ... ESCAPE,SOUNDS LIKE.
Examples#
SELECT 1 <=> NULL, NULL <=> NULL, 'a' = 'A', '10' < '9', 10 < 9;
SELECT id FROM e WHERE sal BETWEEN 100 AND 200;
SELECT id FROM e WHERE dept IN (1, 2);
SELECT id FROM e WHERE sal >= ALL (SELECT sal FROM e WHERE dept = 2);
SELECT id FROM e WHERE (dept, sal) = (2, 150);
SELECT (1, 2) < (1, 3); -- 1