SQLite

SQLite

SQLite operators are reserved words or characters used in SQLite statements when we use WHERE clause to perform operations like comparisons and arithmetic operations.

Operators can be used to specify conditions and as conjunction for multiple conditions in SQLite statements.

There are mainly 4 type of operators in SQLite:

- Arithmetic operators
- Comparison operators
- Logical operators
- Bitwise operators

The following table specifies the different arithmetic operators in SQLite. In this table, we have two variables "a" and "b" holding values 50 and 100 respectively.

Operator | Description | Example |
---|---|---|

+ | Addition Operator: It is used to add the values of both side of the operator. | a+b = 150 |

- | Subtraction Operator: It is used to subtract the right hand operand from left hand operand. | a-b = -50 |

* | Multiplication Operator: It is used to multiply the values of both sides. | a*b = 5000 |

/ | Division Operator: It is used to divide left hand operand by right hand operand. | a/b = 0.5 |

% | Modulus Operator: It is used to divide left hand operand by right hand operand and returns remainder. | b/a = 0 |

The following table specifies the different comparison operators in SQLite. In this table, we have two variables "a" and "b" holding values 50 and 100 respectively.

Operator | Description | Example |
---|---|---|

== | It is used to check if the values of two operands are equal or not, if yes then condition becomes true. | (a == b) is not true. |

= | It is used to check if the values of two operands are equal or not, if yes then condition becomes true. | (a = b) is not true. |

!= | It is used to check if the values of two operands are equal or not, if values are not equal then condition becomes true. | (a != b) is true. |

<> | It is used to check if the values of two operands are equal or not, if values are not equal then condition becomes true. | (a <> b) is true. |

> | It is used to check if the values of left operand is greater than the value of right operand, if yes then condition becomes true. | (a > b) is not true. |

< | It is used to check if the values of left operand is less than the value of right operand, if yes then condition becomes true. | (a < b) is true. |

>= | It is used to check 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. |

<= | It is used to check 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. |

!< | It is used to check 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. |

!> | It is used to check 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. |

Following is a list of logical operators 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 that are within a set of values, given the minimum value and the maximum value. |

EXISTS | The EXISTS operator is used to search for the presence of a row in a specified table that meets certain criteria. |

IN | The IN operator is used to compare a value to a list of literal values that have been specified. |

NOT IN | It is the negation of IN operator which is used to compare a value to a list of literal values that have been 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. Also, glob is case sensitive, unlike like. |

NOT | The NOT operator reverses the meaning of the logical operator with which it is used. For example: EXISTS, NOT BETWEEN, NOT IN, etc. These are known as negate 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 work like = |

IS NOT | The IS NOT operator work like != |

|| | This operator is used to add two different strings and make new one. |

UNIQUE | The UNIQUE operator searches every row of a specified table for uniqueness (no duplicates). |

SQLite Bitwise operators work on bits and perform bit by bit operation.

See the truth table for Binary AND (&) and Binary OR (|):

p | q | p&q | p|q |
---|---|---|---|

0 | 0 | 0 | 0 |

0 | 1 | 0 | 1 |

1 | 1 | 1 | 1 |

1 | 0 | 0 | 1 |

Let's assume two variables "a" and "b", having values 60 and 13 respectively. So Binary values of a and b are:

a= 0011 1100

b= 0000 1101

a&b = 0000 1100

a|b = 0011 1101

~a = 1100 0011

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. | (~a ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number. |

<< | Binary Left Shift Operator. The left operands 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 operands value is moved right by the number of bits specified by the right operand. | a >> 2 will give 15 which is 0000 1111 |

Related Tutorial