Professional Documents
Culture Documents
SQLite expressions
functions that are interpreted (evaluated) according to the particular rules of precedence and of
association for a particular programming language, which computes and then produces (returns, in a
stateful environment) another value. The expression is said to evaluate to that value. (wikipedia)
Literal values
A literal value is a constant of some kind. Literal values may be integers, floating point numbers,
3|Wolf|34.5
Here we return three literals. Namely integer, string and floating point constants.
NULL
The .nullvalue command tells the SQLite to show NULL values as NULL. SQLite shows empty strings
BLOB literals are string literals containing hexadecimal data and preceded by a single "x" or "X"
character.
Operators
Operators are used to build expressions. SQL operators are very similar to mathematical operators.
There are two kinds of operators. Binary and unary. Binary operators work with two operands, unary
work with one. An operator may have one or two operands. An operand is one of the inputs
(arguments) of an operator.
• Arithmetic operators
• Boolean operators
• Relational operators
• Bitwise operators
• Other operators
||
* / %
+ -
OR
Operators are arranged according to the precedence. The || operator has the highest order of
- + ~ NOT
The + is a no-op. It does not do anything. The - unary operator changes positive values to negative and
vice versa.
41
The result is 41. The other two operators will be discussed later on.
Arithmetic operators
Arithmetic operators understood by SQLite are: multiplication, division, addition, subtraction and
modulo.
These are multiplication and division operators, that we know from mathematics.
sqlite> SELECT 3 + 4 - 1 + 5;
11
Addition and subtraction operators.
sqlite> SELECT 11 % 3;
The % operator is called the modulo operator. It finds the remainder of division of one number by
Boolean operators
With boolean operators we perform logical operations. SQLite has the following boolean operators:
AND, OR and NOT. Boolean operators return true or false. In SQLite, 1 is true, 0 is false.
0|0|0|1
The first three operations evaluate to false, the last one to true.
sqlite> SELECT 0 OR 0, 0 OR 1, 1 OR 0, 1 OR 1;
0|1|1|1
The first operation evaluates to false, other operations evaluate to true.
The NOT operator is negation operator. It makes true false and false true.
0|1
Relational operators
Relational operators are used to compare values. These operators always result in boolean value.
Symbol Meaning
< strictly less than
<= less than or equal to
> greater than
>= greater than or equal to
= or == equal to
!= or <> not equal to
1|1
Bitwise operators
Decimal numbers are natural to humans. Binary numbers are native to comptuters. Binary, octal,
decimal or hexadecimal symbols are only notations of the same number. Bitwise operators work with
bits of a binary number. We have binary logical operators and shift operators.
The bitwise and operator performs bit-by-bit comparison between two nubmers. The result for a bit
00110
& 00011
= 00010
The bitwise or operator performs bit-by-bit comparison between two nubmers. The result for a bit
00110
| 00011
= 00111
sqlite> SELECT 6 | 3;
00110
>> 00001
= 00011
We shift each of the bits of number six to the right. It is equal to dividing the six by 2. The result is
00011 or decimal 3.
00110
<< 00001
= 01100
We shift each of the bits of number six to the left. It is equal to muliplying the number six by 2. The
>>> ~7
-8
>>> ~-8
The operator reverts all bits of a number 7. One of the bits also determines, whether the number is
negative or not. If we negate all the bits one more time, we get number 7 again.
Other operators
There are some other operators left. These include ||, IN, LIKE, GLOB, BETWEEN.
wolfhound
Here we check, if the string value 'Tom' is in the list of names, following the IN operator. The return
Id Name Cost
1 Audi 52642
2 Mercedes 57127
3 Skoda 9000
4 Volvo 29000
5 Bentley 350000
6 Citroen 21000
7 Hummer 41400
8 Volkswagen 21600
In the second case, the IN operator allows you to specify multiple values in a WHERE clause.
Id Name Cost
---------- ---------- ----------
1 Audi 52642
7 Hummer 41400
From the Cars table, we choose cars, that are listed after the IN operator.
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
Id Name Cost
4 Volvo 29000
8 Volkswagen 21600
Id Name Cost
1 Audi 52642
Here we select a car name, that has exactly four characters. There are four underscores.
The GLOB operator is similar to the LIKE, but uses the Unix file globbing syntax for its wildcards. Also,
GLOB is case sensitive, unlike LIKE.
Id Name Cost
6 Citroen 21000
8 Volkswagen 21600
Id Name Cost
1 Audi 52642
Here we again select a car name, that has exactly four characters.
Id Name Cost
8 Volkswagen 21600
These two statements demonstrate, that the LIKE operator is case insensitive, GLOB is case sensitive.
sqlite> SELECT * FROM Cars WHERE Cost BETWEEN 20000 AND 55000;
Id Name Cost
1 Audi 52642
4 Volvo 29000
6 Citroen 21000
7 Hummer 41400
8 Volkswagen 21600
In this SQL statement, we have selected cars, which cost between 20000 and 55000 units.
Precedence
The operator precedence tells us which operators are evaluated first. The precedence level is
Like in mathematics, the multiplication operator has a higher precedence than addition operator. So
(3 + 5) * 5
To change the order of evaluation, we can use square brackets. Expressions inside square brackets are
28|40
The first expression evaluates to 28, because the multiplication operator has a higher precedence, than
the addition one. In the second example, we have used square brackets to change the order of
unary + - ~ NOT
||
* / %
+ -
OR
The operators on the same row have the same level of precedence. The precedence grows from bottom
to top.
Associativity
Sometimes the precedence is not satisfactory to determine the outcome of an expression. There is
another rule called associativity. The associativity of operators determines the order of evaluation of
9 / 3 * 3
What is the outcome of this expression? 9 or 1? The multiplication, deletion and the modulo operator
are left to right associated. So the expression is evaluated this way: (9 / 3) * 3 and the result is 9.
sqlite> SELECT 9 / 3 * 3;
The associativity rule is again left to right. If it was right to left, the result would be 0.
Arithmetic, boolean, relational and bitwise operators are all left to right associated.
In this part of the SQLite tutorial, we have covered the SQLite expressions.