Skip to content

SQLite - Operators

An operator is a reserved word or character used primarily in a WHERE clause to perform operations, such as comparisons and arithmetic. Operators are the building blocks for filtering data and creating complex conditions in your queries.

SQLite supports four main types of operators:

  • Arithmetic operators
  • Comparison operators
  • Logical operators
  • Bitwise operators

These operators perform mathematical calculations on numeric values.

OperatorDescriptionExample
+AdditionSELECT 10 + 5; returns 15
-SubtractionSELECT 10 - 5; returns 5
*MultiplicationSELECT 10 * 5; returns 50
/DivisionSELECT 10 / 5; returns 2
%Modulus (remainder of division)SELECT 10 % 3; returns 1

These operators compare two expressions and return a boolean value (true or false). They are essential for WHERE clauses.

OperatorDescriptionExample
= or ==Equal to. Both are aliases.... WHERE price = 100;
!= or <>Not equal to. Both are aliases.... WHERE status != 'shipped';
>Greater than... WHERE quantity > 50;
<Less than... WHERE age < 18;
>=Greater than or equal to... WHERE score >= 90;
<=Less than or equal to... WHERE discount <= 0.5;

These operators combine multiple boolean conditions to form more complex filters.

OperatorDescription
ANDReturns true if all conditions are true. ... WHERE price < 100 AND in_stock = 1
ORReturns true if any of the conditions are true. ... WHERE country = 'USA' OR country = 'Canada'
NOTReverses the result of a condition. ... WHERE NOT country = 'Germany'
BETWEENChecks if a value is within a range (inclusive). ... WHERE price BETWEEN 50 AND 100 is equivalent to price >= 50 AND price <= 100
INChecks if a value matches any value in a list. ... WHERE status IN ('pending', 'processing')
LIKEPerforms pattern matching on strings using wildcards (% for any sequence of characters, _ for a single character).
GLOBSimilar to LIKE, but uses Unix-style wildcards (* for any sequence, ? for a single character) and is case-sensitive.
IS NULLChecks if a value is NULL. ... WHERE shipping_date IS NULL
IS NOT NULLChecks if a value is not NULL. ... WHERE shipping_date IS NOT NULL
EXISTSChecks for the existence of rows in a subquery. Often used for efficient joins.
||String Concatenation. SELECT 'Hello' || ' ' || 'World'; returns ‘Hello World’. Note: In standard SQL, this is a logical OR.

Bitwise operators perform operations on the individual bits of integer values. They are used in specialized applications like manipulating permission flags.

Assume integer A = 60 (binary 00111100) and B = 13 (binary 00001101):

OperatorDescriptionExample
& (Bitwise AND)Sets a bit to 1 if it is 1 in both operands.(A & B) results in 12 (binary 00001100)
| (Bitwise OR)Sets a bit to 1 if it is 1 in either operand.(A | B) results in 61 (binary 00111101)
~ (Bitwise NOT)Inverts all the bits of the operand.(~A) results in -61, due to two’s complement representation of signed integers.
<< (Left Shift)Shifts bits to the left, filling with zeros on the right.A << 2 results in 240 (binary 11110000)
>> (Right Shift)Shifts bits to the right, discarding bits from the right.A >> 2 results in 15 (binary 00001111)