SQLite operators
What are SQLite operators? The
operator is a reserved word or character, mainly used to perform operations in the WHERE clause of SQLite statements, such as comparisons and arithmetic operations.
Operators are used to specify conditions in SQLite statements and connect multiple conditions in a statement.
Arithmetic operators
Comparison operators
Logical operators
Bitwise operators
SQLite arithmetic operators
Assume variable a=10, variable b=20, then:
Operator | Description | Example |
---|---|---|
+ | Addition - operator Adding the values on both sides | a + b will give 30 |
- | Subtraction - left operand minus right operand | a - b will get -10 |
* | Multiplication - multiply the values on both sides of the operator | a * b will get 200 |
/ | Division - Dividing the left operand by the right operand | b / a will give 2 |
% | Modulo - the remainder obtained after dividing the left operand by the right operand | b % a will give 0 |
Example
The following is a simple example of SQLite arithmetic operators:
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:
Description | Example | ||
---|---|---|---|
Checks whether the values of the two operands are equal, if so the condition is true. | (a == b) is not true. | ||
Checks whether the values of the two operands are equal. If they are equal, the condition is true. | (a = b) is not true. | ||
Checks whether the values of the two operands are equal. If they are not equal, the condition is true. | (a != b) is true. | ||
Checks whether the values of the two operands are equal. If they are not equal, the condition is true. | (a <> b) is true. | ||
Checks whether 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. | ||
Checks whether 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 whether 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 whether 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. | ||
Checks whether 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. | ||
Checks whether 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. |
Operator | Description |
---|---|
AND | AND operator allows The presence of multiple conditions in the WHERE clause of the statement. |
BETWEEN | The BETWEEN operator is used to search for a value within a range of values within a given minimum and maximum range. |
EXISTS | The EXISTS operator is used to search for the presence of rows in a specified table that meet certain conditions. |
IN | The IN operator compares a value to a specified list of values. |
NOT IN | The opposite of the IN operator, used to compare a value with values that are not in a specified list. |
LIKE | The LIKE operator compares a value to similar values using the wildcard operator. |
GLOB | The GLOB operator compares a value to similar values using the wildcard operator. GLOB differs from LIKE in that it is case-sensitive. |
NOT | The NOT operator is the opposite of the logical operator used. Such as NOT EXISTS, NOT BETWEEN, NOT IN, etc. It is a negation operator. |
OR | The OR operator is used to combine multiple conditions in the WHERE clause of a SQL statement. |
IS NULL | The NULL operator compares a value to a NULL value. |
IS | The IS operator is similar to =. |
IS NOT | IS NOT operator is similar to !=. |
|| | Connect two different strings to get a new string. |
UNIQUE | The UNIQUE operator searches every row in the specified table for uniqueness (no duplicates). |
Example
Assume that the COMPANY table has the following records:
1 Paul 32 California 20000.0
2 Allen 25 Texas 15000.0
3 . 27 Texas 85000.0
6 Kim 22 South-Hall 45000.0
7 James 24 H ouston 10000.0
The following example demonstrates the usage of SQLite logical operators.
The following SELECT statement lists all records where AGE is greater than or equal to 25 and salary is greater than or equal to 65000.00:
ID ----------------------------------------------------------------------------------------—
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0
salary greater than or equal to 65000.00:
sqlite> SELECT * FROM COMPANY WHERE AGE >= 25 OR SALARY >= 65000;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 whose AGE is not NULL. The result displays all records, which means that no record has an AGE equal to NULL:
ID NAME AGE ADDRESS SALARY
----------- ----------- ---------- ---- ------ ----------
1 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 whose NAME starts with 'Ki'. The characters after 'Ki' are not restricted:
6 Kim 22 South-Hall 45000.0
The following SELECT statement lists all records with an AGE value of 25 or 27:
ID ------ ----------
2 David 27 Texas 85000.0The following SELECT statement lists all records whose AGE value is neither 25 nor 27:
ID name Age address salary
----------------------------------------------------------------------------------- -----------------1#1 PAUL 32 California 20000.0
3 Teddy 23 Norway 20000.0
6 Kim 22 SOUTH-HALL 45000.0 ## 7 James 24 HOUSTON 1000000.0
## This ##The following SELECT statement lists all records with AGE values between 25 and 27:
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----- -----5 David
The following SELECT statement uses a SQL subquery. The subquery searches for all records with AGE fields in SALARY > 65000. The following WHERE clause is used with the EXISTS operator to list the AGEs in the outer query that exist in All records in the results returned by the subquery:
AGE
- ---------
32
25
23
25
27
22
24
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
1 Paul 32 CALIFORNIA 20000.0
q | p & q | p | q | |
---|---|---|---|
0 | 0 | 0 | |
1 | 0 | 1 | |
1 | 1 | 1 | |
0 | 0 | 1 |
Operator | Description | Instance |
---|---|---|
& | The binary AND operator copies one bit to the result if both operands are present at the same time. | (A & B) will get 12, which is 0000 1100 |
| | If present in any operand, binary OR operation character is copied into the result. | (A | B) will get 61, which is 0011 1101 |
The two's complement operator is a unary operator with " Flip" bit effect. | (~A ) will get -61, which is 1100 0011, 2's complement, signed binary number. | |
Binary left shift operator. The value of the left operand is shifted left by the number of bits specified by the right operand. | A << 2 will get 240, which is 1111 0000 | |
Binary right shift operator. The value of the left operand is shifted to the right by the number of bits specified by the right operand. | A >> 2 will get 15, which is 0000 1111 |
sqlite> .mode line
sqlite> select 60 | 13;sqlite> select 60 & 13;
60 & 13 = 12
sqlite> select 60 ^ 13;
10 * 20 = 200
sqlite> select (~60);
(~ 60) = -61
sqlite> select (60 << 2);
(60 << 2) = 240
sqlite> select (60 >> ; 2);
(60 >> 2) = 15
##