Expressions
Expressions are combinations of values, operators, and functions that Turso evaluates to produce a result. Expressions appear in many SQL clauses including SELECT columns, WHERE conditions, ORDER BY, GROUP BY, HAVING, CHECK constraints, and DEFAULT values.Literals
Numeric Literals
String Literals
String literals are enclosed in single quotes. To include a single quote within a string, use two consecutive single quotes:Blob Literals
Blob literals are hexadecimal strings preceded byx or X:
NULL Literal
Boolean Literals
Turso does not have a separate boolean type. Use integers0 (false) and 1 (true):
Operators
Arithmetic Operators
| Operator | Description | Example | Result |
|---|---|---|---|
+ | Addition | 3 + 4 | 7 |
- | Subtraction | 10 - 3 | 7 |
* | Multiplication | 3 * 4 | 12 |
/ | Division | 10 / 3 | 3 (integer division) |
- (unary) | Negation | -5 | -5 |
+ (unary) | No-op | +5 | 5 |
CAST or multiply by 1.0 for floating-point division:
Comparison Operators
| Operator | Description | Example |
|---|---|---|
= or == | Equal | x = 5 |
!= or <> | Not equal | x != 5 |
< | Less than | x < 5 |
> | Greater than | x > 5 |
<= | Less than or equal | x <= 5 |
>= | Greater than or equal | x >= 5 |
1 (true), 0 (false), or NULL (if either operand is NULL).
Logical Operators
| Operator | Description | Example |
|---|---|---|
AND | Logical AND | x > 0 AND x < 10 |
OR | Logical OR | x = 1 OR x = 2 |
NOT | Logical NOT | NOT x = 5 |
Bitwise Operators
| Operator | Description | Example | Result |
|---|---|---|---|
& | Bitwise AND | 5 & 3 | 1 |
| | Bitwise OR | 5 | 3 | 7 |
~ | Bitwise NOT | ~5 | -6 |
<< | Left shift | 1 << 4 | 16 |
>> | Right shift | 16 >> 2 | 4 |
String Concatenation
| Operator | Description | Example | Result |
|---|---|---|---|
|| | Concatenation | 'hello' || ' ' || 'world' | 'hello world' |
CAST Expression
The CAST expression converts a value to a specified type.| Parameter | Type | Description |
|---|---|---|
| expression | any | The value to convert |
| type-name | type | The target type name |
Turso Extension: In STRICT tables with custom types,
CAST(value AS custom_type) applies the custom type’s ENCODE function. See CREATE TYPE for details.COLLATE Expression
The COLLATE expression specifies a collation sequence for string comparison.| Collation | Description |
|---|---|
BINARY | Byte-by-byte comparison (default) |
NOCASE | Case-insensitive comparison for ASCII characters |
RTRIM | Like BINARY but ignores trailing spaces |
Pattern Matching
LIKE Operator
The LIKE operator performs case-insensitive pattern matching (for ASCII characters). The% wildcard matches any sequence of characters, and _ matches any single character.
GLOB Operator
The GLOB operator performs case-sensitive pattern matching using Unix-style wildcards.* matches any sequence of characters, and ? matches any single character.
REGEXP Operator
The REGEXP operator performs regular expression matching. Requires the regexp extension (loaded by default).BETWEEN Expression
The BETWEEN expression tests whether a value falls within an inclusive range.expression >= low AND expression <= high:
IN Expression
The IN expression tests whether a value matches any value in a list or subquery result.EXISTS Expression
The EXISTS expression returns1 if the subquery returns at least one row, and 0 otherwise.
IS NULL / IS NOT NULL
The IS NULL expression tests whether a value is NULL. Unlike= NULL, which always returns NULL, IS NULL returns 1 or 0.
IS DISTINCT FROM
The IS DISTINCT FROM expression compares two values, treating NULL as a comparable value.CASE Expression
The CASE expression provides conditional logic within SQL expressions.Simple CASE
Searched CASE
Scalar Subqueries
A subquery enclosed in parentheses that returns a single value can be used as an expression.RAISE Function
The RAISE function raises an error condition. In standard SQLite, RAISE can only be used inside triggers. In Turso,RAISE(ABORT, msg) can also be used outside triggers — in CHECK constraints, custom type ENCODE expressions, and standalone queries. The other forms (RAISE(IGNORE), RAISE(ROLLBACK, msg), RAISE(FAIL, msg)) are only valid inside triggers.
| Form | Description |
|---|---|
RAISE(IGNORE) | Abandon the current trigger action but continue the statement |
RAISE(ROLLBACK, msg) | Abort the statement and roll back the current transaction |
RAISE(ABORT, msg) | Abort the current statement; prior changes in the transaction are preserved |
RAISE(FAIL, msg) | Abort the current statement at the current point; prior row changes are preserved |
Operator Precedence
Operators are evaluated in the following order (highest precedence first):| Precedence | Operators |
|---|---|
| 1 (highest) | ~ (unary NOT), + (unary), - (unary) |
| 2 | || (concatenation) |
| 3 | *, / |
| 4 | +, - |
| 5 | <<, >>, &, | |
| 6 | <, <=, >, >= |
| 7 | =, ==, !=, <>, IS, IS NOT, IS DISTINCT FROM, IN, LIKE, GLOB, REGEXP, BETWEEN |
| 8 | NOT |
| 9 | AND |
| 10 (lowest) | OR |
See Also
- Data Types for storage classes and type affinity
- Scalar Functions for built-in functions usable in expressions
- SELECT for using expressions in queries