SQLite AND/OR Operators
SQLite's AND and OR operators are used to compile multiple conditions to narrow down the data selected in SQLite statements. These two operators are known as conjunction operators.
These operators provide the possibility of multiple comparisons between different operators within the same SQLite statement.
AND Operator
The AND operator allows the existence of multiple conditions in the WHERE clause of an SQL statement. When using the AND operator, the entire condition is true only if all conditions are true. For example, [condition1] AND [condition2] is true only if both condition1 and condition2 are true.
Syntax
The basic syntax of the AND operator with a WHERE clause is as follows:
SELECT column1, column2, columnN
FROM table_name
WHERE [condition1] AND [condition2]...AND [conditionN];
You can use the AND operator to combine N number of conditions. The action required by the SQLite statement, whether it is a transaction or a query, is that all conditions separated by AND must be true.
Example
Suppose the COMPANY table has the following records:
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
1 Paul 32 California 20000.0
2 Allen 25 Texas 15000.0
3 Teddy 23 Norway 20000.0
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0
6 Kim 22 South-Hall 45000.0
7 James 24 Houston 10000.0
The following SELECT statement lists all records where AGE is greater than or equal to 25 and SALARY is greater than or equal to 65000.00:
sqlite> SELECT * FROM COMPANY WHERE AGE >= 25 AND SALARY >= 65000;
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0
OR Operator
The OR operator is also used to combine multiple conditions in the WHERE clause of an SQL statement. When using the OR operator, the entire condition is true if any one of the conditions is true. For example, [condition1] OR [condition2] is true if either condition1 or condition2 is true.
Syntax
The basic syntax of the OR operator with a WHERE clause is as follows:
SELECT column1, column2, columnN
FROM table_name
WHERE [condition1] OR [condition2]...OR [conditionN]
You can use the OR operator to combine N number of conditions. The action required by the SQLite statement, whether it is a transaction or a query, is that any one of the conditions separated by OR must be true.
Example
Suppose the COMPANY table has the following records:
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
1 Paul 32 California 20000.0
2 Allen 25 Texas 15000.0
3 Teddy 23 Norway 20000.0
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0
6 Kim 22 South-Hall 45000.0
7 James 24 Houston 10000.0
The following SELECT statement lists all records where AGE is greater than or equal to 25 or SALARY is greater than or equal to 65000.00:
sqlite> SELECT * FROM COMPANY WHERE AGE >= 25 OR SALARY >= 65000;
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
1 Paul 32 California 20000.0
2 Allen 25 Texas 15000.0
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0