Sqlite Or Condition

SQLite: OR Condition

This SQLite post explains how to use the SQLite OR condition with syntax and examples.

Description

The SQLite OR Condition is used to test two or more conditions where records are returned when any one of the conditions are met. It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.

Syntax

The syntax for the OR Condition in SQLite is:

WHERE condition1
OR condition2
...
OR condition_n;

Parameters or Arguments

condition1, condition2, ... condition_n

Any of the conditions that must be met for the records to be selected.

Note

  • The SQLite OR condition allows you to test 2 or more conditions.
  • The SQLite OR condition requires that any of the conditions (ie: condition1, condition2, condition_n) be must be met for the record to be included in the result set.

Example - With SELECT Statement

The first SQLite OR condition example that we'll take a look at involves a SELECT statement with 2 conditions:

SELECT *
FROM employees
WHERE last_name = 'Smith'
OR employee_id = 5;

This SQLite OR condition example would return all employees that have a last_name of 'Smith' or an employee_id of 5. Because the * is used in the SELECT statement, all fields from the employees table would appear in the result set.

Example - With SELECT Statement (3 conditions)

The next SQLite OR example looks at a SELECT statement with 3 conditions. If any of these conditions is met, the record will be included in the result set.

SELECT last_name, first_name
FROM employees
WHERE last_name = 'Smith'
OR employee_id = 5
OR first_name = 'Sarah';

This SQLite OR condition example would return all last_name and first_name values from the employees table where the last_name is 'Smith', the employee_id is 5, or the first_name is 'Sarah'. If any of these conditions is met, the record will be included in the result set.

Example - With INSERT Statement

The SQLite OR condition can be used in the INSERT statement.

For example:

INSERT INTO contacts
(contact_id, last_name, first_name)
SELECT employee_id, last_name, first_name
FROM employees
WHERE employee_id = 1
OR employee_id = 2;

This SQLite OR 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 either 1 or 2.

Example - With UPDATE Statement

The SQLite OR condition can be used in the UPDATE statement.

For example:

UPDATE employees
SET department = 'Accounting'
WHERE last_name = 'Smith'
OR first_name = 'Jane';

This SQLite OR condition example would update all department values in the employees table to 'Accounting' where the last_name is 'Smith' or the first_name is 'Jane'.

Example - With DELETE Statement

The SQLite OR condition can be used in the DELETE statement.

For example:

DELETE FROM employees
WHERE employee_id = 1
OR first_name = 'Joanne';

This SQLite OR condition example would delete all employees from the employees table where the employee_id is 1 or the first_name is 'Joanne'.