SQLite Operators

What is an SQLite Operator?

An operator is a reserved word or character primarily used in the WHERE clause of an SQLite statement to perform operations, such as comparisons and arithmetic operations.

Operators are used to specify conditions in an SQLite statement and to combine multiple conditions in the statement.

  • Arithmetic Operators

  • Comparison Operators

  • Logical Operators

  • Bitwise Operators

SQLite Arithmetic Operators

Assume variable a=10 and variable b=20, then:

OperatorDescriptionExample
+Addition - Adds values on either side of the operatora + b will give 30
-Subtraction - Subtracts right operand from left operanda - b will give -10
*Multiplication - Multiplies values on either side of the operatora * b will give 200
/Division - Divides left operand by right operandb / a will give 2
%Modulus - Gives the remainder after dividing left operand by right operandb % a will give 0

Examples

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

SQLite Comparison Operators

Assume variable a=10 and variable b=20, then:

OperatorDescriptionExample
==Checks if the values of two operands are equal, if yes then condition becomes true.(a == b) is not true.
=Checks if the values of two operands are equal, if yes then condition becomes true.(a = b) is not true.
!=Checks if the values of two operands are equal, if not then condition becomes true.(a != b) is true.
<>Checks if the values of two operands are equal, if not then condition becomes true.(a <> b) is true.
>Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.(a > b) is not true.
<Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.(a < b) is true.
>=Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.(a >= b) is not true.
<=Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.(a <= b) is true.
!<Checks if the value of left operand is not less than the value of right operand, if yes then condition becomes true.(a !< b) is false.
!>Checks if the value of left operand is not greater than the value of right operand, if yes then condition becomes true.(a !> b) is true.

Examples

Assume 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 theWHERE clause, which will be explained in a separate chapter later, but for now you need to understand that the WHERE clause is used to set conditions in a SELECT statement.

The following SELECT statement lists all records where SALARY is greater than 50,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 all records where SALARY is equal to 20,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 all records where SALARY is not equal to 20,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 all records where SALARY is not equal to 20,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 all records where SALARY is greater than or equal to 65,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

SQLite Logical Operators

The following is a list of all logical operators in SQLite.

OperatorDescription
ANDThe AND operator allows the existence of multiple conditions in the WHERE clause of an SQL statement.
BETWEENThe BETWEEN operator is used to search for values within a range of values, given a minimum and a maximum value.
EXISTSThe EXISTS operator is used to search for the existence of a row in a specified table that meets certain criteria.
INThe IN operator is used to compare a value to a list of specified values.
NOT INThe opposite of the IN operator, used to compare a value to values that are not in a specified list.
LIKEThe LIKE operator is used to compare a value to similar values using wildcard operators.
GLOBThe GLOB operator is used to compare a value to similar values using wildcard operators. GLOB is different from LIKE in that it is case-sensitive.
NOTThe NOT operator is the opposite of the logical operator used. For example, NOT EXISTS, NOT BETWEEN, NOT IN, etc.It is a negating operator.
ORThe OR operator is used to combine multiple conditions in the WHERE clause of an SQL statement.
IS NULLThe NULL operator is used to compare a value with a NULL value.
ISThe IS operator is similar to =.
IS NOTThe IS NOT operator is similar to !=.
||Concatenates two different strings to get a new string.
UNIQUEThe UNIQUE operator searches each row of a specified table to ensure uniqueness (no duplicates).

Examples

Assume 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 SQLite logical operators.

The following SELECT statement lists all records where AGE is greater than or equal to 25andand SALARY is greater than or equal to 65000.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 all records where AGE is greater than or equal to 25orand SALARY is greater than or equal to 65000.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, the result shows all records, meaning no record has AGE 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 NAME starts with 'Ki', with no restriction on characters after 'Ki':

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 NAME starts with 'Ki', with no restriction on characters after 'Ki':

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 all records where AGE is 25 or 27:

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 all records where AGE is neither 25 nor 27:

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 all records where AGE is between 25 and 27:

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 all records with AGE fields where SALARY > 65000. The subsequent WHERE clause, used with the EXISTS operator, lists all records in the outer query where AGE exists in the results returned by the subquery:

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 all records with AGE fields where SALARY > 65000. The subsequent WHERE clause, used with the > operator, lists all records in the outer query where AGE is greater than the ages in the results returned by the subquery:

sqlite> SELECT * FROM COMPANY 
        WHERE AGE > (SELECT AGE FROM COMPANY WHERE SALARY > 65000);
ID          NAME        AGE         ADDRESS     SALARY
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0

SQLite Bitwise Operators

Bitwise operators act on bits and perform operations bit by bit. The truth tables for & and | are as follows:

pqp & qp | q
0000
0101
1111
1001

Assume if A = 60 and B = 13, now 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 and variable B=13, then:

OperatorDescriptionExample
&The binary AND operator copies a bit to the result if it exists in both operands.(A & B) will give 12, which is 0000 1100
|The binary OR operator copies a bit to the result if it exists in either operand.(A | B) will give 61, which is 0011 1101
~The binary complement operator is a unary operator that has the effect of "flipping" bits, i.e., 0 becomes 1 and 1 becomes 0.(~A ) will yield -61, which is 1100 0011, the two's complement form of a 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 yield 240, which is 1111 0000
>>Binary right shift operator. The value of the left operand is shifted right by the number of bits specified by the right operand.A >> 2 will yield 15, which is 0000 1111

Examples

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
Other Extensions