Small. Fast. Reliable.
Choose any three.
Search for:

SQL As Understood By SQLite

[Top]

expression

expr:

R-61970-26480:[ ]

literal-value:

raise-function:

select-stmt:

type-name:

This section is different from the others. Most other sections of this document talks about a particular SQL command. This section does not talk about a standalone command but about "expressions" which are subcomponents of most other commands.

Operators

R-15514-65163:[SQLite understands the following binary operators, in order from highest to lowest precedence:

||
*    /    %
+    -
<<   >>   &    |
<    <=   >    >=
=    ==   !=   <>   IS   IS NOT   IN   LIKE   GLOB   MATCH   REGEXP
AND   
OR
]

R-13958-53419:[Supported unary prefix operators are these:

-    +    ~    NOT
]

R-59577-33471:[The COLLATE operator is a unary postfix operator that assigns a collating sequence to an expression. ] R-36231-30731:[The COLLATE operator has a higher precedence (binds more tightly) than any binary operator and any unary prefix operator except "~". ] (COLLATE and "~" are associative so their binding order does not matter.) R-58731-25439:[The collating sequence set by the COLLATE operator overrides the collating sequence determined by the COLLATE clause in a table column definition. ] See the detailed discussion on collating sequences in the Datatype In SQLite3 document for additional information.

R-53670-03373:[The unary operator + is a no-op. ] R-19480-30968:[It can be applied to strings, numbers, blobs or NULL and it always returns a result with the same value as the operand. ]

Note that there are two variations of the equals and not equals operators. R-03679-60639:[Equals can be either = or ==. ] R-30082-38996:[The non-equals operator can be either != or <>. ] R-44409-62641:[The || operator is "concatenate" - it joins together the two strings of its operands. ] R-08914-63790:[The operator % outputs the value of its left operand modulo its right operand. ]

R-20665-17792:[The result of any binary operator is either a numeric value or NULL, except for the || concatenation operator which always evaluates to either NULL or a text value. ]

R-24731-45773:[The IS and IS NOT operators work like = and != except when one or both of the operands are NULL. ] R-06325-15315:[In this case, if both operands are NULL, then the IS operator evaluates to 1 (true) and the IS NOT operator evaluates to 0 (false). ] R-19812-36779:[If one operand is NULL and the other is not, then the IS operator evaluates to 0 (false) and the IS NOT operator is 1 (true). ] R-61975-13410:[It is not possible for an IS or IS NOT expression to evaluate to NULL. ] R-38759-38789:[Operators IS and IS NOT have the same precedence as =. ]

Literal Values (Constants)

A literal value represents a constant. R-31536-32008:[Literal values may be integers, floating point numbers, strings, BLOBs, or NULLs. ]

The syntax for integer and floating point literals (collectively "numeric literals") is shown by the following diagram:

numeric-literal:

R-31352-57778:[ ]

R-59165-19659:[If a numeric literal has a decimal point or an exponentiation clause or if its magnitude is less than -9223372036854775808 or greater than 9223372036854775807, then it is a floating point literal. Otherwise is it is an integer literal. ] R-31163-29022:[The "E" character that begins the exponentiation clause of a floating point literal can be either upper or lower case. ] R-15867-30493:[The "." character is always used as the decimal point even if the locale setting specifies "," for this role - the use of "," for the decimal point would result in syntactic ambiguity. ]

R-50109-18895:[Hexadecimal integer literals follow the C-language notation of "0x" or "0X" followed by hexadecimal digits. ] R-12628-51384:[For example, 0x1234 means the same as 4660 and 0x8000000000000000 means the same as -9223372036854775808. ] R-42106-50385:[Hexadecimal integer literals are interpreted as 64-bit two's-complement integers and are thus limited to sixteen significant digits of precision. ] Support for hexadecimal integers was added to SQLite version 3.8.6 (2014-08-15). R-21873-57098:[For backwards compatibility, the "0x" hexadecimal integer notation is only understood by the SQL language parser, not by the type conversions routines. ] R-47301-17448:[String variables that contain text formatted like hexadecimal integers are not interpreted as hexadecimal integers when coercing the string value into an integer due to a CAST expression or for a column affinity transformation or prior to performing a numeric operation or for any other run-time conversions. ] R-43895-16198:[When coercing a string value in the format of a hexadecimal integer into an integer value, the conversion process stops when the 'x' character is seen so the resulting integer value is always zero. ] R-05658-50861:[SQLite only understands the hexadecimal integer notation when it appears in the SQL statement text, not when it appears as part of the content of the database. ]

R-35229-17830:[A string constant is formed by enclosing the string in single quotes ('). ] R-07100-06606:[A single quote within the string can be encoded by putting two single quotes in a row - as in Pascal. ] C-style escapes using the backslash character are not supported because they are not standard SQL.

R-09593-03321:[BLOB literals are string literals containing hexadecimal data and preceded by a single "x" or "X" character. ] R-19836-11244:[Example: X'53514C697465' ]

R-23914-51476:[A literal value can also be the token "NULL". ]

Parameters

A "variable" or "parameter" token specifies a placeholder in the expression for a value that is filled in at runtime using the sqlite3_bind() family of C/C++ interfaces. Parameters can take several forms:

?NNN R-33509-39458:[A question mark followed by a number NNN holds a spot for the NNN-th parameter. NNN must be between 1 and SQLITE_MAX_VARIABLE_NUMBER. ]
? R-33670-36097:[A question mark that is not followed by a number creates a parameter with a number one greater than the largest parameter number already assigned. ] R-42938-07030:[If this means the parameter number is greater than SQLITE_MAX_VARIABLE_NUMBER, it is an error. ] This parameter format is provided for compatibility with other database engines. But because it is easy to miscount the question marks, the use of this parameter format is discouraged. Programmers are encouraged to use one of the symbolic formats below or the ?NNN format above instead.
:AAAA R-11620-22743:[A colon followed by an identifier name holds a spot for a named parameter with the name :AAAA. ] R-11370-04520:[Named parameters are also numbered. The number assigned is one greater than the largest parameter number already assigned. ] R-42620-22184:[If this means the parameter would be assigned a number greater than SQLITE_MAX_VARIABLE_NUMBER, it is an error. ] To avoid confusion, it is best to avoid mixing named and numbered parameters.
@AAAA R-49783-61279:[An "at" sign works exactly like a colon, except that the name of the parameter created is @AAAA. ]
$AAAA R-62610-51329:[A dollar-sign followed by an identifier name also holds a spot for a named parameter with the name $AAAA. ] R-55025-21042:[The identifier name in this case can include one or more occurrences of "::" and a suffix enclosed in "(...)" containing any text at all. ] This syntax is the form of a variable name in the Tcl programming language. The presence of this syntax results from the fact that SQLite is really a Tcl extension that has escaped into the wild.

R-14068-49671:[Parameters that are not assigned values using sqlite3_bind() are treated as NULL. ] The sqlite3_bind_parameter_index() interface can be used to translate a symbolic parameter name into its equivalent numeric index.

R-38091-32352:[The maximum parameter number is set at compile-time by the SQLITE_MAX_VARIABLE_NUMBER macro. ] R-47512-27714:[An individual database connection D can reduce its maximum parameter number below the compile-time maximum using the sqlite3_limit(D, SQLITE_LIMIT_VARIABLE_NUMBER,...) interface. ]

The LIKE, GLOB, REGEXP, and MATCH operators

R-16584-60189:[The LIKE operator does a pattern matching comparison. ] R-11295-04657:[The operand to the right of the LIKE operator contains the pattern and the left hand operand contains the string to match against the pattern. ] R-55406-38524:[A percent symbol ("%") in the LIKE pattern matches any sequence of zero or more characters in the string. ] R-30433-25443:[An underscore ("_") in the LIKE pattern matches any single character in the string. ] R-59007-20454:[Any other character matches itself or its lower/upper case equivalent (i.e. case-insensitive matching). ] Important Note: R-23648-58527:[SQLite only understands upper/lower case for ASCII characters by default. ] R-04532-11527:[The LIKE operator is case sensitive by default for unicode characters that are beyond the ASCII range. ] For example, R-44381-11669:[the expression 'a' LIKE 'A' is TRUE but 'æ' LIKE 'Æ' is FALSE. ] The ICU extension to SQLite includes an enhanced version of the LIKE operator that does case folding across all unicode characters.

R-56683-13731:[If the optional ESCAPE clause is present, then the expression following the ESCAPE keyword must evaluate to a string consisting of a single character. ] R-02045-23762:[This character may be used in the LIKE pattern to include literal percent or underscore characters. ] R-13345-31830:[The escape character followed by a percent symbol (%), underscore (_), or a second instance of the escape character itself matches a literal percent symbol, underscore, or a single escape character, respectively. ]

R-51359-17496:[The infix LIKE operator is implemented by calling the application-defined SQL functions like(Y,X) or like(Y,X,Z). ]

R-22868-25880:[The LIKE operator can be made case sensitive using the case_sensitive_like pragma. ]

R-52087-12043:[The GLOB operator is similar to LIKE but uses the Unix file globbing syntax for its wildcards. ] R-09813-17279:[Also, GLOB is case sensitive, unlike LIKE. ] R-39616-20555:[Both GLOB and LIKE may be preceded by the NOT keyword to invert the sense of the test. ] R-39414-35489:[The infix GLOB operator is implemented by calling the function glob(Y,X) and can be modified by overriding that function. ]

R-33693-50180:[The REGEXP operator is a special syntax for the regexp() user function. ] R-41650-20872:[No regexp() user function is defined by default and so use of the REGEXP operator will normally result in an error message. ] R-65524-61849:[If an application-defined SQL function named "regexp" is added at run-time, then the "X REGEXP Y" operator will be implemented as a call to "regexp(Y,X)". ]

R-37916-47407:[The MATCH operator is a special syntax for the match() application-defined function. ] R-42037-37826:[The default match() function implementation raises an exception and is not really useful for anything. ] R-06021-09373:[But extensions can override the match() function with more helpful logic. ]

The BETWEEN operator

R-40079-54503:[The BETWEEN operator is logically equivalent to a pair of comparisons. "x BETWEEN y AND z" is equivalent to "x>=y AND x<=z" except that with BETWEEN, the x expression is only evaluated once. ] R-05155-34454:[The precedence of the BETWEEN operator is the same as the precedence as operators == and != and LIKE and groups left to right. ]

The CASE expression

A CASE expression serves a role similar to IF-THEN-ELSE in other programming languages.

The optional expression that occurs in between the CASE keyword and the first WHEN keyword is called the "base" expression. R-15199-61389:[There are two basic forms of the CASE expression: those with a base expression and those without. ]

R-30638-59954:[In a CASE without a base expression, each WHEN expression is evaluated and the result treated as a boolean, starting with the leftmost and continuing to the right. ] R-39009-25596:[The result of the CASE expression is the evaluation of the THEN expression that corresponds to the first WHEN expression that evaluates to true. ] R-24227-04807:[Or, if none of the WHEN expressions evaluate to true, the result of evaluating the ELSE expression, if any. ] R-14168-07579:[If there is no ELSE expression and none of the WHEN expressions are true, then the overall result is NULL. ]

R-13943-13592:[A NULL result is considered untrue when evaluating WHEN terms. ]

R-38620-19499:[In a CASE with a base expression, the base expression is evaluated just once and the result is compared against the evaluation of each WHEN expression from left to right. ] R-07667-49537:[The result of the CASE expression is the evaluation of the THEN expression that corresponds to the first WHEN expression for which the comparison is true. ] R-47543-32145:[Or, if none of the WHEN expressions evaluate to a value equal to the base expression, the result of evaluating the ELSE expression, if any. ] R-54721-48557:[If there is no ELSE expression and none of the WHEN expressions produce a result equal to the base expression, the overall result is NULL. ]

R-11479-62774:[When comparing a base expression against a WHEN expression, the same collating sequence, affinity, and NULL-handling rules apply as if the base expression and WHEN expression are respectively the left- and right-hand operands of an = operator. ]

R-37304-39405:[If the base expression is NULL then the result of the CASE is always the result of evaluating the ELSE expression if it exists, or NULL if it does not. ]

R-56280-17369:[Both forms of the CASE expression use lazy, or short-circuit, evaluation. ]

R-34773-62253:[The only difference between the following two CASE expressions is that the x expression is evaluated exactly once in the first example but might be evaluated multiple times in the second:

]

The IN and NOT IN operators

R-15810-56691:[The IN and NOT IN operators take an expression on the left and a list of values or a subquery on the right. ] R-01921-64245:[When the right operand of an IN or NOT IN operator is a subquery, the subquery must have the same number of columns as there are columns in the row value of the left operand. ] R-35033-20570:[The subquery on the right of an IN or NOT IN operator must be a scalar subquery if the left expression is not a row value expression. ] R-05985-29284:[If the right operand of an IN or NOT IN operator is a list of values, each of those values must be scalars and the left expression must also be a scalar. ] R-29609-10377:[The right-hand side of an IN or NOT IN operator can be a table name or table-valued function name in which case the right-hand side is understood to be subquery of the form "(SELECT * FROM name)". ] R-52275-55503:[When the right operand is an empty set, the result of IN is false and the result of NOT IN is true, regardless of the left operand and even if the left operand is NULL. ]

R-50221-42915:[The result of an IN or NOT IN operator is determined by the following matrix:

Left operand
is NULL
Right operand
contains NULL
Right operand
is an empty set
Left operand found
within right operand
Result of
IN operator
Result of
NOT IN operator
no no no no false true
does not matter no yes no false true
no does not matter no yes true false
no yes no no NULL NULL
yes does not matter no does not matter NULL NULL
]

R-13595-45863:[Note that SQLite allows the parenthesized list of scalar values on the right-hand side of an IN or NOT IN operator to be an empty list but most other SQL database database engines and the SQL92 standard require the list to contain at least one element. ]

Table Column Names

R-06726-07466:[A column name can be any of the names defined in the CREATE TABLE statement or one of the following special identifiers: "ROWID", "OID", or "_ROWID_". ] R-07387-29430:[The three special identifiers describe the unique integer key (the rowid) associated with every row of every table and so are not available on WITHOUT ROWID tables. ] R-44615-33286:[The special identifiers only refer to the row key if the CREATE TABLE statement does not define a real column with the same name. ] R-46129-45598:[The rowid can be used anywhere a regular column can be used. ]

The EXISTS operator

R-25588-27181:[The EXISTS operator always evaluates to one of the integer values 0 and 1. ] R-58553-63740:[If executing the SELECT statement specified as the right-hand operand of the EXISTS operator would return one or more rows, then the EXISTS operator evaluates to 1. ] R-19673-40972:[If executing the SELECT would return no rows at all, then the EXISTS operator evaluates to 0. ]

R-35109-49139:[The number of columns in each row returned by the SELECT statement (if any) and the specific values returned have no effect on the results of the EXISTS operator. ] R-10645-12439:[In particular, rows containing NULL values are not handled any differently from rows without NULL values. ]

Subquery Expressions

R-43573-23448:[A SELECT statement enclosed in parentheses is a subquery. ] R-56294-03966:[All types of SELECT statement, including aggregate and compound SELECT queries (queries with keywords like UNION or EXCEPT) are allowed as scalar subqueries. ] R-18318-14995:[The value of a subquery expression is the first row of the result from the enclosed SELECT statement. ] R-15900-52156:[In other words, an implied "LIMIT 1" is added to the subquery, overriding an explicitly coded LIMIT. ] R-52325-25449:[The value of a subquery expression is NULL if the enclosed SELECT statement returns no rows. ]

A subquery that returns a single column is a scalar subquery and can be used most anywhere. R-22239-33740:[A subquery that returns two or more columns is a row value subquery and can only be used as the operand of a comparison operator. ]

Correlated Subqueries

R-25616-08788:[A SELECT statement used as either a scalar subquery or as the right-hand operand of an IN, NOT IN or EXISTS expression may contain references to columns in the outer query. ] Such a subquery is known as a correlated subquery. R-45820-61953:[A correlated subquery is reevaluated each time its result is required. ] R-35325-25517:[An uncorrelated subquery is evaluated only once and the result reused as necessary. ]

CAST expressions

A CAST expression of the form "CAST(expr AS type-name)" is used to convert the value of expr to a different storage class specified by type-name. R-20854-17109:[A CAST conversion is similar to the conversion that takes place when a column affinity is applied to a value except that with the CAST operator the conversion always takes place even if the conversion lossy and irreversible, whereas column affinity only changes the data type of a value if the change is lossless and reversible. ]

R-32434-09092:[If the value of expr is NULL, then the result of the CAST expression is also NULL. ] R-29283-15561:[Otherwise, the storage class of the result is determined by applying the rules for determining column affinity to the type-name. ]

Affinity of type-name Conversion Processing
NONE R-43522-35548:[Casting a value to a type-name with no affinity causes the value to be converted into a BLOB. ] R-22956-37754:[Casting to a BLOB consists of first casting the value to TEXT in the encoding of the database connection, then interpreting the resulting byte sequence as a BLOB instead of as TEXT. ]
TEXT R-04207-37981:[To cast a BLOB value to TEXT, the sequence of bytes that make up the BLOB is interpreted as text encoded using the database encoding. ]

R-22235-47006:[Casting an INTEGER or REAL value into TEXT renders the value as if via sqlite3_snprintf() except that the resulting TEXT uses the encoding of the database connection. ]

REAL R-26346-36443:[When casting a BLOB value to a REAL, the value is first converted to TEXT. ]

R-54898-34554:[When casting a TEXT value to REAL, the longest possible prefix of the value that can be interpreted as a real number is extracted from the TEXT value and the remainder ignored. ] R-11321-47427:[Any leading spaces in the TEXT value are ignored when converging from TEXT to REAL. ] R-22662-28218:[If there is no prefix that can be interpreted as a real number, the result of the conversion is 0.0. ]

INTEGER R-21829-14563:[When casting a BLOB value to INTEGER, the value is first converted to TEXT. ]

R-47612-45842:[When casting a TEXT value to INTEGER, the longest possible prefix of the value that can be interpreted as an integer number is extracted from the TEXT value and the remainder ignored. ] R-34400-33772:[Any leading spaces in the TEXT value when converting from TEXT to INTEGER are ignored. ] R-43164-44276:[If there is no prefix that can be interpreted as an integer number, the result of the conversion is 0. ] R-08980-53124:[The CAST operator understands decimal integers only — conversion of hexadecimal integers stops at the "x" in the "0x" prefix of the hexadecimal integer string and thus result of the CAST is always zero. ]

R-02752-50091:[A cast of a REAL value into an INTEGER results in the integer between the REAL value and zero that is closest to the REAL value. ] R-51517-40824:[If a REAL is greater than the greatest possible signed integer (+9223372036854775807) then the result is the greatest possible signed integer and if the REAL is less than the least possible signed integer (-9223372036854775808) then the result is the least possible signed integer. ]

Prior to SQLite version 3.8.2 (2013-12-06), casting a REAL value greater than +9223372036854775807.0 into an integer resulted in the most negative integer, -9223372036854775808. This behavior was meant to emulate the behavior of x86/x64 hardware when doing the equivalent cast.

NUMERIC R-09295-61337:[Casting a TEXT or BLOB value into NUMERIC first does a forced conversion into REAL but then further converts the result into INTEGER if and only if the conversion from REAL to INTEGER is lossless and reversible. ] This is the only context in SQLite where the NUMERIC and INTEGER affinities behave differently.

R-30347-18702:[Casting a REAL or INTEGER value to NUMERIC is a no-op, even if a real value could be losslessly converted to an integer. ]

R-64550-29191:[Note that the result from casting any non-BLOB value into a BLOB and the result from casting any BLOB value into a non-BLOB value may be different depending on whether the database encoding is UTF-8, UTF-16be, or UTF-16le. ]

Boolean Expressions

The SQL language features several contexts where an expression is evaluated and the result converted to a boolean (true or false) value. These contexts are:

R-48118-34840:[To convert the results of an SQL expression to a boolean value, SQLite first casts the result to a NUMERIC value in the same way as a CAST expression. A numeric zero value (integer value 0 or real value 0.0) is considered to be false. A NULL value is still NULL. All other values are considered true. ]

R-62477-06476:[For example, the values NULL, 0.0, 0, 'english' and '0' are all considered to be false. ] R-55532-10108:[Values 1, 1.0, 0.1, -0.1 and '1english' are considered to be true. ]

Functions

SQLite supports many simple and aggregate SQL functions. For presentation purposes, simple functions are further subdivided into core functions and date-time functions. Applications can add new functions, written in C/C++, using the sqlite3_create_function() interface.

R-59344-06440:[It is possible to have an aggregate function with the same name as a simple function, as long as the number of arguments for the two forms of the function are different. ] R-47913-44653:[For example, the max() function with a single argument is an aggregate and the max() function with two or more arguments is a simple function. ]