Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

SQLite Operators

SQLite Operators

What are SQLite operators?

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

Operators are used to specify conditions in a SQLite statement and to connect multiple conditions in a statement.

SQLite Arithmetic Operators

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

operator Description Example
+ Addition - adds the values on both sides of the operator a + b will give 30
- Subtraction - subtracts the right operand from the left operand a - b will give -10
* Multiplication - multiplies the values on both sides of the operator a * b will give 200
/ Division - divides the left operand by the right operand b / a will give 2
% Modulus - the remainder after dividing the left operand by the right operand b % a will give 0

Example

The following are simple examples 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:

operator Description Example
== Checks whether the values of the two operands are equal; if equal, the condition is true. (a == b) is not true.
= Checks whether the values of the two operands are equal; if equal, the condition is true. (a = b) is not true.
!= Checks whether the values of the two operands are equal; if not equal, the condition is true. (a != b) is true.
<> Checks whether the values of the two operands are equal; if 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.
>= Checks 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.
<= Checks 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.

Example

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 use of various SQLite comparison operators.

Here, we use the **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 conditions for the 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 equals 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.

operator Description
AND AND operator allows the presence of multiple conditions in the WHERE clause of a SQL statement.
BETWEEN BETWEEN operator is used to search for values within a range between a given minimum and maximum value.
EXISTS EXISTS operator is used to search for the existence of a row in a specified table that satisfies a certain condition.
IN IN operator is used to compare a value with a series of specified list values.
NOT IN IN operator's opposite, used to compare a value with values not in a series of specified lists.
LIKE LIKE operator is used to compare a value with similar values using wildcard operators.
GLOB GLOB operator is used to compare a value with similar values using wildcard operators. GLOB differs from LIKE in that it is case-sensitive.
NOT NOT operator is the opposite of the logical operator used. For example NOT EXISTS, NOT BETWEEN, NOT IN, and so on.It is the negation operator.
OR OR operator is used to combine multiple conditions in the WHERE clause of a SQL statement.
IS NULL NULL operator is used to compare a value with a NULL value.
IS IS operator is similar to =.
IS NOT IS NOT operator is similar to !=.
|| Concatenates two different strings to produce a new string.
UNIQUE UNIQUE operator searches every row in the specified table to ensure uniqueness (no duplicates).

Example

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

The following SELECT statement lists all records with AGE greater than or equal to 25 andand salary 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 with AGE greater than or equal to 25 orand salary 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'; characters after 'Ki' are not restricted:



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'; characters after 'Ki' are not restricted:



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 that finds all records with an AGE field where SALARY > 65000; the following WHERE clause is used with the EXISTS operator to list all records in the outer query whose 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 that finds all records with an AGE field where SALARY > 65000; the following WHERE clause is used with the > operator to list all records in the outer query whose 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 operate on bits and perform operations bit by bit. The truth tables for & 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 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:

operator Description Example
& If present in both operands, the binary AND operator copies a bit to the result. (A & B) will give 12, which is 0000 1100
| If present in either operand, the binary OR operator copies a bit to the result. (A | B) will give 61, which is 0011 1101
~ The binary complement operator is a unary operator with a "flip" bit effect, that is, 0 becomes 1 and 1 becomes 0. (~A ) will give -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 give 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 give 15, which is 0000 1111

Example

The following examples demonstrate the use 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