YouTip LogoYouTip

Sqlite Operators

SQLite Operators

Operators are reserved words or characters, primarily used in the WHERE clause of SQLite statements to perform operations such as comparisons and arithmetic.

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

  • Arithmetic Operators
  • Comparison Operators
  • Logical Operators
  • Bitwise Operators

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

Operator Description Example
+ Addition - Adds values on both sides of the operator a + b will give 30
- Subtraction - Subtracts right operand from left operand a - b will give -10
* Multiplication - Multiplies values on both sides of the operator a * b will give 200
/ Division - Divides left operand by right operand b / a will give 2
% Modulus - Gives the remainder after integer division of left operand by right operand b % a will give 0

Examples

Here 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

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

Operator Description Example
== Checks if the values of two operands are equal or not. If yes, then condition becomes true. (a == b) is not true.
= Checks if the values of two operands are equal or not. If yes, then condition becomes true. (a = b) is not true.
!= Checks if the values of two operands are equal or not. If values are not equal, then condition becomes true. (a != b) is true.
<> Checks if the values of two operands are equal or not. If values are not equal, 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 are using the WHERE 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 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 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

Below is a list of all logical operators available in SQLite.

Operator Description
AND The AND operator allows the existence of multiple conditions in an SQL statement's WHERE clause.
BETWEEN The BETWEEN operator is used to search for values within a set of given minimum and maximum values.
EXISTS The EXISTS operator is used to search for the presence of a row in a specified table that meets certain conditions.
IN The IN operator is used to compare a value to a specified list of values.
NOT IN The opposite of the IN operator, used to compare a value to a list of values that are not specified.
LIKE The LIKE operator is used to compare a value to similar values using wildcard operators.
GLOB The GLOB operator is used to compare a value to similar values using wildcard operators. 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 an SQL statement's WHERE clause.
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 make a new string.
UNIQUE The UNIQUE operator searches every row of a specified table for 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 25 AND 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 25 OR 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, showing 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 value is either 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 value 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 value 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 a SQL subquery. The subquery finds all records with the AGE field where SALARY > 65000. The subsequent WHERE clause, used with the EXISTS operator, lists all records from the outer query where the 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 a SQL subquery. The subquery finds all records with the AGE field where SALARY > 65000. The subsequent WHERE clause, used with the > operator, lists all records from the outer query where the AGE is greater than the ages 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

Bitwise operators work on bits and perform bit-by-bit operations. 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 will be 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 SQLite language. Assume variable A=60 and variable B=13, then:

Operator Description Example
& Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) will give 12, which is 0000 1100
| Binary OR Operator copies a bit if it exists in either operand. (A | B) will give 61, which is 0011 1101
~ Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. 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 left operand's value is moved 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 left operand's value is moved right by the number of bits specified by the right operand. A >> 2 will give 15, which is 0000 1111

Examples

The following examples demonstrate 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 < select (60 >> 2);
(60 >> 2) = 15
← Sqlite ExpressionsSqlite Select β†’