The SQLite IN
operator is used to check if a given value is contained within a set of values
.
When you run a query
with the IN operator, the given column will be checked and if at least one value from the set of values is found, then the query will one or more records.
Syntax
The SQLite IN syntax is as follows:
SELECT column_name1, column_name2, ... FROM table_name WHERE column_name IN (set_of_values);
Example
Table of student_address
address_id | city | country |
---|---|---|
100 | San Antonio | US |
101 | San Jose | US |
102 | Philadelphia | US |
103 | Austin | US |
104 | Boston | US |
105 | Seattle | US |
In the first example, the select returns records from the student_address table, if the address_id column will have values included in the given set.
SELECT * FROM student_address WHERE address_id IN (101,102);
Output
address_id | city | country |
---|---|---|
101 | San Jose | US |
102 | Philadelphia | US |
The second example is similar to the first, the difference is that the city column will be checked. If at least one value from the set of values is found in the city column, then the select will return at least one record.
SELECT * FROM student_address WHERE city IN ('Boston','Seattle');
Output
address_id | city | country |
---|---|---|
104 | Boston | US |
105 | Seattle | US |