SQL reference (MySQL and MariaDB dialects)

Expressions and operator precedence

What an expression can contain, how operators group, and where Raft DB's precedence or evaluation differs from MySQL.

An expression is a literal, a column name, a variable, a function call, a subquery, or those joined by operators and parentheses.

Syntax#

SQL
expr:
    literal
  | column_name | table.column | db.table.column
  | @user_variable | @@system_variable | @user_variable := expr
  | function(args)
  | aggregate(...) [OVER (...)]
  | CASE ... END
  | CAST(expr AS type) | CONVERT(expr, type)
  | EXTRACT(unit FROM expr) | TRIM([BOTH | LEADING | TRAILING] [str] FROM expr)
  | INTERVAL expr unit
  | (expr) | (expr, expr, ...)               -- a row constructor
  | (SELECT ...) | EXISTS (SELECT ...)
  | expr op expr | op expr | expr IS [NOT] {NULL | TRUE | FALSE | UNKNOWN}
  | expr [NOT] {IN (list | subquery) | BETWEEN expr AND expr | LIKE pattern | REGEXP pattern}
  | expr COLLATE collation

Operator precedence#

From the tightest to the loosest. Operators on one line group from left to right.

LevelOperators
1INTERVAL expr unit
2COLLATE, BINARY, ->, ->>, MEMBER OF (the JSON operators are MySQL only)
3unary -, +, ~
4^
5*, /, DIV, %, MOD
6-, +
7<<, >>
8&
9|
10=, <=>, >=, >, <=, <, <>, !=, IS, LIKE, REGEXP, IN, BETWEEN
11NOT, !
12AND, &&
13XOR, OR, ||
14:=

2 + 3 & 4 is (2 + 3) & 4 (5 & 4 is 4); 1 | 2 & 3 is 1 | (2 & 3); 2 * 3 ^ 2 is 2 * (3 ^ 2).

Differences from MySQL's table:

  • XOR is read at the level of OR, not between OR and AND. Use parentheses when you mix them.
  • ! is read at the level of NOT (MySQL gives it the tightest level of the unary operators); write (!a) = b with parentheses.
  • || is always OR. The mode PIPES_AS_CONCAT is accepted and has no effect; use CONCAT.
  • The mode HIGH_NOT_PRECEDENCE has no effect: NOT a BETWEEN b AND c is NOT (a BETWEEN b AND c).
  • BINARY expr is read and ignored (see Cast and conversion functions); a binary or case-sensitive COLLATE (utf8mb4_bin, ..._cs) is refused as not supported; the case-insensitive collations are read and change nothing.
  • LIKE ... ESCAPE is not supported yet (use the default escape, the backslash).

Evaluation rules#

  • Three-valued logic. A comparison with NULL is NULL; NULL AND 0 is 0, NULL OR 1 is 1; see NULL handling.
  • Division. / is exact: 7 / 2 is 3.5000 (a decimal with the dividend's scale plus div_precision_increment, 4 by default, rounded half away from zero); DIV is the integer division; division by zero and MOD by zero give NULL.
  • Numbers and text. In an arithmetic context text converts to a number ('10' + 5 is 15; text that does not start with digits is 0). In a comparison of two texts the texts are compared, so '10' < '9' is 1; a number and a text are compared as numbers (see Type conversion).
  • Exactness. + - * / % DIV on whole numbers and decimals are exact up to 128 bits; a float or double operand makes the result a double.
  • Row constructors. (a, b) = (1, 2), <>, <, <=, >, >=, <=> and (a, b) IN ((1, 2), (3, 4)) compare part by part (all equal; one differs; the first pair that differs decides). Rows with different numbers of parts are error 1241 (The operand must have n column(s)). A row can also be compared with a subquery.
  • ANY, SOME, ALL. x op ANY (subquery); see Comparison operators.
  • Types of expressions. The type of a computed value (for CREATE TABLE ... AS SELECT and functional indexes) is worked out as MySQL does: INT or BIGINT by width for whole-number arithmetic, DECIMAL(p+4, s+4) for /, DOUBLE for float and text operands, merged types for COALESCE, IF, CASE, GREATEST. This is checked against MySQL 9.7 for about 120 expressions. Not typed yet: DATE_ADD and interval arithmetic, window functions, expressions over derived tables, views, natural joins and UNION.

Examples#

SQL
SELECT 5 & 4, 5 | 2, 5 ^ 1, 1 << 3, 256 >> 2, ~0, 2 + 3 & 4, 1 | 2 & 3, 2 * 3 ^ 2;
SELECT '10' < '9', 10 < 9, '10' + 5, 'a' = 'A', NULL IS NULL, 1 <=> NULL, 7 DIV 2;
SELECT (1, 2) = (1, 2), (1, 3) < (1, 4), (2, 3) IN ((1, 2), (2, 3));

See also#