Expressions & operators
Operator precedence
Section titled “Operator precedence”Expressions evaluate with standard precedence, from tightest to loosest:
- Parentheses and unary
+/-/NOT *,/,%+,-,||- comparison
=,<>/!=,<,<=,>,>= NOTANDOR
IS NULL, IN, BETWEEN, and LIKE are predicates parsed at the comparison level.
Arithmetic
Section titled “Arithmetic”| Operator | Meaning |
|---|---|
+ - * |
numeric |
/ |
division — integer when both operands are integers (truncates toward zero) |
% |
modulo |
| ` |
- Arithmetic on a
NULLoperand yieldsNULL. - Division or modulo by zero returns
NULL, not an error. - Integer operands produce integer results; mixing a real promotes to real. Strings that parse as numbers are coerced.
||renders both sides as text and never propagatesNULL:NULL || 'x'→'x'.
Comparison
Section titled “Comparison”=, <> (or !=), <, <=, >, >= follow three-valued logic: if either operand is NULL, the result is NULL (which behaves as false in a WHERE filter).
Comparison first tries to compare numerically (if both sides parse as numbers) and falls back to lexicographic string comparison otherwise.
Logical operators
Section titled “Logical operators”AND and OR implement SQL three-valued logic:
| TRUE | FALSE | NULL | |
|---|---|---|---|
TRUE AND |
TRUE | FALSE | NULL |
FALSE AND |
FALSE | FALSE | FALSE |
NULL AND |
NULL | FALSE | NULL |
TRUE OR |
TRUE | TRUE | TRUE |
FALSE OR |
TRUE | FALSE | NULL |
NULL OR |
TRUE | NULL | NULL |
NOT NULL is NULL.
Both forms are supported:
-- simple CASESELECT CASE status WHEN 'a' THEN 1 WHEN 'b' THEN 2 ELSE 0 END FROM t;
-- searched CASESELECT CASE WHEN x > 0 THEN 'pos' WHEN x < 0 THEN 'neg' ELSE 'zero' END FROM t;- A simple
CASEwith aNULLoperand matches nothing. - A searched
CASEtreats aNULLcondition as false. - No
ELSEand no match returnsNULL.
CAST(expr AS TYPE)TYPE may be any recognized type name. CAST(NULL AS anything) is NULL. Conversions:
- to
INTEGER-family → truncates toward zero (int64(value)). - to
REAL/FLOAT/DOUBLE/NUMERIC/DECIMAL→ float. - to
TEXT/VARCHAR/CHAR→ string rendering. - Any other target returns the value unchanged.
Predicates
Section titled “Predicates”IS NULL
Section titled “IS NULL”x IS NULLx IS NOT NULLx IN (1, 2, 3)x IN (SELECT ...) -- exactly one columnx NOT IN (...)Full three-valued logic: NULL IN (...) is NULL; an empty list is FALSE for IN and TRUE for NOT IN; a list containing NULL with no match yields NULL.
BETWEEN
Section titled “BETWEEN”x BETWEEN low AND highx NOT BETWEEN low AND highEquivalent to x >= low AND x <= high (respectively x < low OR x > high) with three-valued NULL handling.
x LIKE 'pattern'x NOT LIKE 'pattern'x LIKE 'pattern' ESCAPE 'char'%matches any sequence,_matches a single character.- Matching is case-insensitive (both sides are lowercased).
- The
ESCAPEclause is parsed but ignored — the escape character is treated literally.
EXISTS
Section titled “EXISTS”EXISTS (SELECT ...)Returns TRUE if the subquery returns at least one row, including for correlated subqueries.
Subqueries
Section titled “Subqueries”- Scalar subqueries return the first column of the first row, or
NULLwhen empty. Unlike strict SQL, a multi-row scalar subquery returns the first value rather than erroring. - Correlated subqueries reference outer columns and are re-evaluated per outer row.
- Non-correlated
IN (SELECT ...)results are cached for the duration of the enclosing query. - A small, specialized optimization decorrelates scalar aggregate subqueries of the form
(SELECT COUNT(*)/SUM/AVG/MIN/MAX(col) FROM inner WHERE inner.key = outer.key).
Column references
Section titled “Column references”- Unqualified references resolve against the visible tables; ambiguous references across joined tables are an analysis error.
- Qualified references use
table.columnoralias.column. rowid,oid, and_rowid_resolve to the implicit rowid.
Constant folding
Section titled “Constant folding”A WHERE clause that references no columns (e.g. WHERE 1 = 1) is evaluated once; a constant-false WHERE short-circuits to an empty result (or a single aggregated row for aggregate queries). This is an optimization, not a guarantee, and doesn’t imply a general planner.