This SQLite post explores all of the comparison operators used to test for equality and inequality, as well as the more advanced operators.
Comparison operators are used in the WHERE clause to determine which records to select. Here is a list of the comparison operators that you can use in SQLite:
Comparison Operator | Description |
---|---|
= | Equal |
== | Equal |
> | Not Equal |
!= | Not Equal |
> | Greater Than |
>= | Greater Than or Equal |
Less Than | |
= | Less Than or Equal |
IN ( ) | Matches a value in a list |
NOT | Negates a condition |
BETWEEN | Within a range (inclusive) |
IS NULL | NULL value |
IS NOT NULL | Non-NULL value |
LIKE | Pattern matching with % and _ |
EXISTS | Condition is met if subquery returns at least one row |
Some of these operators are fairly straight forward and others are more complicated. Let's begin by reviewing the easier comparison operators in SQLite.
In SQLite, you can use the = operator to test for equality in a query.
For example:
In this example, the SELECT statement above would return all rows from the employees table where the favorite_website is equal to 'AODBA.com'.
Or you could also write this query using the == operator, as follows:
Both of these queries would return the same results.
In SQLite, you can use the > or != operators to test for inequality in a query.
For example, we could test for inequality using the > operator, as follows:
In this example, the SELECT statement would return all rows from the employees table where the first_name is not equal to Joanne.
Or you could also write this query using the != operator, as follows:
Both of these queries would return the same results.
You can use the > operator in SQLite to test for an expression greater than.
In this example, the SELECT statement would return all rows from the employees table where the employee_id is greater than 25. An employee_id equal to 25 would not be included in the result set.
In SQLite, you can use the >= operator to test for an expression greater than or equal to.
In this example, the SELECT statement would return all rows from the employees table where the employee_id is greater than or equal to 25. In this case, n employee_id equal to 25 would be included in the result set.
You can use the operator in SQLite to test for an expression less than.
In this example, the SELECT statement would return all rows from the customers table where the customer_id is less than 300. A customer_id equal to 300 would not be included in the result set.
In SQLite, you can use the = operator to test for an expression less than or equal to.
In this example, the SELECT statement would return all rows from the customers table where the customer_id is less than or equal to 300. In this case, customer_id equal to 300 would be included in the result set.