English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
An operator is a reserved word or character, mainly used in the WHERE clause of SQLite statements to perform operations such as comparison and arithmetic.
Operators are used to specify conditions in SQLite statements and to connect multiple conditions in the statement.
Arithmetic operators
Comparison operators
Logical operators
Bitwise operators
Assume variable a=10, variable b=20, then:
Operator | Description | Example |
---|---|---|
+ | Addition - Add the values on both sides of the operator | a + b will get 30 |
- | Subtraction - Subtract the right operand from the left operand | a - b will get -10 |
* | Multiplication - Multiply the values on both sides of the operator | a * b will get 200 |
/ | Division - The left operand divided by the right operand | b / a will get 2 |
% | Modulus - The remainder obtained by dividing the left operand by the right operand | b % a will give 0 |
The following is a simple example of SQLite arithmetic operators:
sqlite> .mode line sqlite> select 10 + 20; 10 + 20 = 30 sqlite> select 10 - 20; 10 - 20 = -10 sqlite> select 10 * 20; 10 * 20 = 200 sqlite> select 10 / 5; 10 / 5 = 2 sqlite> select 12 % 5; 12 % 5 = 2
Assume variable a=10, variable b=20, then:
Operator | Description | Example |
---|---|---|
== | Check if the values of the two operands are equal, if equal, the condition is true. | (a == b) is not true. |
= | Check if the values of the two operands are equal, if equal, the condition is true. | (a = b) is not true. |
!= | Check if the values of the two operands are equal, if not equal, the condition is true. | (a != b) is true. |
<> | Check if the values of the two operands are equal, if not equal, the condition is true. | (a <> b) is true. |
> | Check if the value of the left operand is greater than the value of the right operand, if so, the condition is true. | (a > b) is not true. |
< | Check if the value of the left operand is less than the value of the right operand, if so, the condition is true. | (a < b) is true. |
>= | Check if the value of the left operand is greater than or equal to the value of the right operand, if so, the condition is true. | (a >= b) is not true. |
<= | Check if the value of the left operand is less than or equal to the value of the right operand, if so, the condition is true. | (a <= b) is true. |
!< | Check if the value of the left operand is not less than the value of the right operand, if so, the condition is true. | (a !< b) is false. |
!> | Check if the value of the left operand is not greater than the value of the right operand, if so, the condition is true. | (a !> b) is true. |
Assuming the COMPANY table has the following records:
ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 1 Paul 32 California 20000.0 2 Allen 25 Texas 15000.0 3 Teddy 23 Norway 20000.0 4 Mark 25 Rich-Mond 65000.0 5 David 27 Texas 85000.0 6 Kim 22 South-Hall 45000.0 7 James 24 Houston 10000.0
The following examples demonstrate the usage of various SQLite comparison operators.
Here, we use WHERE Clause, which will be explained in a separate chapter later, but now you need to understand that the WHERE clause is used to set the conditions of the SELECT statement.
The following SELECT statement lists the SALARY that is greater than 5All records with: 0,000.00
sqlite> SELECT * FROM COMPANY WHERE SALARY > 50000; ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 4 Mark 25 Rich-Mond 65000.0 5 David 27 Texas 85000.0
The following SELECT statement lists the SALARY that is equal to 2All records with: 0,000.00
sqlite> SELECT * FROM COMPANY WHERE SALARY = 20000; ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 1 Paul 32 California 20000.0 3 Teddy 23 Norway 20000.0
The following SELECT statement lists the SALARY that is not equal to 2All records with: 0,000.00
sqlite> SELECT * FROM COMPANY WHERE SALARY != 20000; ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 2 Allen 25 Texas 15000.0 4 Mark 25 Rich-Mond 65000.0 5 David 27 Texas 85000.0 6 Kim 22 South-Hall 45000.0 7 James 24 Houston 10000.0
The following SELECT statement lists the SALARY that is not equal to 2All records with: 0,000.00
sqlite> SELECT * FROM COMPANY WHERE SALARY <> 20000; ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 2 Allen 25 Texas 15000.0 4 Mark 25 Rich-Mond 65000.0 5 David 27 Texas 85000.0 6 Kim 22 South-Hall 45000.0 7 James 24 Houston 10000.0
The following SELECT statement lists the SALARY that is greater than or equal to 65All records with: ,000.00
sqlite> SELECT * FROM COMPANY WHERE SALARY >= 65000; ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 4 Mark 25 Rich-Mond 65000.0 5 David 27 Texas 85000.0
Below is a list of all logical operators in SQLite.
Operator | Description |
---|---|
AND | The AND operator allows for the existence of multiple conditions in a WHERE clause of an SQL statement. |
BETWEEN | The BETWEEN operator is used to search for values within a range of specified minimum and maximum values. |
EXISTS | The EXISTS operator is used to search for the existence of rows in a specified table that meet certain conditions. |
IN | The IN operator is used to compare a value with a series of specified list values. |
NOT IN | The opposite of the IN operator, it is used to compare a value with a value that is not in a specified list of values. |
LIKE | The LIKE operator is used to compare a value with a similar value that uses a wildcard operator. |
GLOB | The GLOB operator is used to compare a value with a similar value using a wildcard operator. Unlike LIKE, GLOB is case-sensitive. |
NOT | The NOT operator is the opposite of the logical operators used. For example, NOT EXISTS, NOT BETWEEN, NOT IN, etc.It is the negation operator. |
OR | The OR operator is used to combine multiple conditions in the WHERE clause of an SQL statement. |
IS NULL | The NULL operator is used to compare a value with a NULL value. |
IS | The IS operator is similar to =. |
IS NOT | The IS NOT operator is similar to !=. |
|| | Concatenates two different strings to create a new string. |
UNIQUE | The UNIQUE operator searches each row in the specified table to ensure uniqueness (no duplicates). |
Assuming the COMPANY table has the following records:
ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 1 Paul 32 California 20000.0 2 Allen 25 Texas 15000.0 3 Teddy 23 Norway 20000.0 4 Mark 25 Rich-Mond 65000.0 5 David 27 Texas 85000.0 6 Kim 22 South-Hall 45000.0 7 James 24 Houston 10000.0
The following example demonstrates the usage of SQLite logical operators.
The following SELECT statement lists the values of AGE that are greater than or equal to 25 andSalary is greater than or equal to 65All records with a salary of 000.00:
sqlite> SELECT * FROM COMPANY WHERE AGE >= 25 AND SALARY >= 65000; ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 4 Mark 25 Rich-Mond 65000.0 5 David 27 Texas 85000.0
The following SELECT statement lists the values of AGE that are greater than or equal to 25 orSalary is greater than or equal to 65All records with a salary of 000.00:
sqlite> SELECT * FROM COMPANY WHERE AGE >= 25 OR SALARY >= 65000; ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 1 Paul 32 California 20000.0 2 Allen 25 Texas 15000.0 4 Mark 25 Rich-Mond 65000.0 5 David 27 Texas 85000.0
The following SELECT statement lists all records where AGE is not NULL, showing all records, meaning that no record's AGE is equal to NULL:
sqlite> SELECT * FROM COMPANY WHERE AGE IS NOT NULL; ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 1 Paul 32 California 20000.0 2 Allen 25 Texas 15000.0 3 Teddy 23 Norway 20000.0 4 Mark 25 Rich-Mond 65000.0 5 David 27 Texas 85000.0 6 Kim 22 South-Hall 45000.0 7 James 24 Houston 10000.0
The following SELECT statement lists all records where the NAME starts with 'Ki', and the characters following 'Ki' are unrestricted:
sqlite> SELECT * FROM COMPANY WHERE NAME LIKE 'Ki%'; ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 6 Kim 22 South-Hall 45000.0
The following SELECT statement lists all records where the NAME starts with 'Ki', and the characters following 'Ki' are unrestricted:
sqlite> SELECT * FROM COMPANY WHERE NAME GLOB 'Ki'*; ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 6 Kim 22 South-Hall 45000.0
The following SELECT statement lists the values of AGE that are 25 or 27 All records:
sqlite> SELECT * FROM COMPANY WHERE AGE IN ( 25, 27 ); ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 2 Allen 25 Texas 15000.0 4 Mark 25 Rich-Mond 65000.0 5 David 27 Texas 85000.0
The following SELECT statement lists the values of AGE that are neither 25 are also not 27 All records:
sqlite> SELECT * FROM COMPANY WHERE AGE NOT IN ( 25, 27 ); ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 1 Paul 32 California 20000.0 3 Teddy 23 Norway 20000.0 6 Kim 22 South-Hall 45000.0 7 James 24 Houston 10000.0
The following SELECT statement lists the values of AGE that are 25 and 27 All records between:
sqlite> SELECT * FROM COMPANY WHERE AGE BETWEEN 25 AND 27; ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 2 Allen 25 Texas 15000.0 4 Mark 25 Rich-Mond 65000.0 5 David 27 Texas 85000.0
The following SELECT statement uses an SQL subquery, which finds SALARY > 65000 records with the AGE field, the WHERE clause following the EXISTS operator lists all records in the outer query where AGE exists in the result of the subquery returned in the age field of the outer query:
sqlite> SELECT AGE FROM COMPANY WHERE EXISTS (SELECT AGE FROM COMPANY WHERE SALARY > 65000); AGE ---------- 32 25 23 25 27 22 24
The following SELECT statement uses an SQL subquery, which finds SALARY > 65000 records with the AGE field, the WHERE clause following the > operator lists all records in the outer query where AGE is greater than the result of the subquery returned in the age field of the outer query:
sqlite> SELECT * FROM COMPANY WHERE AGE > (SELECT AGE FROM COMPANY WHERE SALARY > 65000); ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 1 Paul 32 California 20000.0
Bitwise operators operate on bits and perform operations bit by bit. The truth tables & and | are as follows:
p | q | p & q | p | q |
---|---|---|---|
0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 |
1 | 1 | 1 | 1 |
1 | 0 | 0 | 1 |
Assume if A = 60, and B = 13, now shown in binary format, they are as follows:
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
~A = 1100 0011
The following table lists the bitwise operators supported by the SQLite language. Assume variable A=60, variable B=13, then:
Operator | Description | Example |
---|---|---|
& | If the binary AND operator copies a bit to the result if it exists in both operands. | (A & B) will result in 12,即为 0000 1100 |
| | If the binary OR operator copies a bit to the result if it exists in either operand. | (A | B) will result in 61, which is 0011 1101 |
~ | The binary complement operator is a unary operator with a 'flip' bit effect, that is, 0 becomes1,1to become 0. | (~A) will result in -61, means 1100 0011, a two's complement form of a signed binary number. |
<< | The binary left shift operator. The value of the left operand is shifted to the left by the number of bits specified by the right operand. | A << 2 将得到 240, means 1111 0000 |
>> | 二进制右移运算符。左操作数的值向右移动右操作数指定的位数。 | A >> 2 将得到 15,即为 0000 1111 |
The following example demonstrates the usage of SQLite bitwise operators:
sqlite> .mode line sqlite> select 60 | 13; 60 | 13 = 61 sqlite> select 60 & 13; 60 & 13 = 12 sqlite> select (~60); (~60) = -61 sqlite> select (60 << 2); (60 << 2) = 240 sqlite> select (60 >> 2); (60 >> 2) = 15