SQL reference (MySQL and MariaDB dialects)

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#

SQL
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#

OperatorMeaning
=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 NULLthe value is or is not NULL
IS TRUE, IS FALSE, IS UNKNOWNthe truth of the value; always 0 or 1 (NOT negates)
BETWEEN a AND bx >= 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, = SOMEthe same as IN
<> ALLthe same as NOT IN
op ANY, op SOMEtrue if the comparison is true for at least one row of the answer; an empty answer is false
op ALLtrue 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.
  • NULL gives NULL for all operators except <=>, IS and EXISTS; 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) and x <> ALL (subquery) become IN and NOT IN; the others are the OR (AND) of the comparisons with each row of the answer, with NULLs as in three-valued logic.
  • BETWEEN compares with the loose rules of >= and <=: 'b' BETWEEN 'A' AND 'C' is 1.
  • Indexes. The simple conditions of a WHERE (column op constant, IN, BETWEEN joined by AND) are given to Raft DB, which uses its indexes (hash for = and IN, ordered for ranges); the front-end still checks the whole condition.
  • Not supported yet: LIKE ... ESCAPE, SOUNDS LIKE.

Examples#

SQL
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

See also#