This SQLite post explains how to use the SQLite AND condition with syntax and examples.
The SQLite AND Condition (also called the AND Operator) is used to test two or more conditions in a SELECT, INSERT, UPDATE, or DELETE statement.
The syntax for the AND Condition in SQLite is:
All of the conditions that must be met for the records to be selected.
Let's look at some examples that show how to use the AND condition in SQLite.
The first SQLite AND condition query involves a SELECT statement with 2 conditions.
For example:
This SQLite AND example would return all employees that have a last_name of 'Smith' and have a employee_id less than or equal to 50. Because the * is used in the SELECT statement, all fields from the employees table would appear in the result set.
Our next SQLite AND example shows how the AND condition can be used to join multiple tables in a SELECT statement.
For example:
Though the above SQL works just fine, you would more traditionally write this SQL as follows using a proper INNER JOIN.
For example:
This SQLite AND condition example would return all rows where the employee_id is greater than 100. And the employees and positions tables are joined on the position_id field. You will notice that all of the fields are prefixed with the table names (ie: employees.employee_id). This is required to eliminate any ambiguity as to which field is being referenced; as the same field name can exist in both the employees and the positions tables.
In this case, the result set would only display the employee_id, last_name, and title fields (as listed in the first part of the SELECT statement.).
This next SQLite AND example demonstrates how the AND condition can be used in the INSERT statement.
For example:
This SQLite AND condition example would insert into the contacts table, all employee_id, last_name, and first_name values from the employees table where the employee_id is greater than 20 and less than 100.
This SQLite AND condition example shows how the AND condition can be used in the UPDATE statement.
For example:
This SQLite AND condition example would update all employee_id values in the employees table to 1 where the employee's last_name is 'Smith' and first_name is 'Jane'.
Finally, this last SQLite AND example demonstrates how the AND condition can be used in the DELETE statement.
For example:
This SQLite AND condition example would delete all records from the employees table where the employee_id is greater than 20 and the first_name is 'Sarah'.