How can I include or exclude several exact values with an IN filter?
Quick answer
Use IN to select rows whose value matches any item in a list, such as WHERE status IN ('new', 'paid'). Use NOT IN to select rows matching none of those items. In SQLite, a row with a NULL status passes neither filter in a WHERE clause. If you want NULL-status rows included with an exclusion filter, add OR status IS NULL. [S1]
The question, the details, the next step.
A practical checklist
Use
INfor values to keep andNOT INfor values to exclude.Check the list contains the values you intend to compare.
Decide explicitly whether rows with a NULL column value should be included.
Check a dynamically assembled list for an unintended NULL.
A few more useful answers
Does IN require a row to match every listed value?
No. It matches a row when the compared value equals any item in the list. [S1]
Will NOT IN include a row whose status is NULL?
Not in SQLite: the comparison evaluates to NULL rather than true, so the row does not pass WHERE. Add OR status IS NULL if those rows should be included.
What if the NOT IN list contains NULL?
In SQLite, a NULL in the list can make a nonmatching comparison evaluate to NULL, so it may not pass WHERE. [S1]
Sources
References used for this guide.
- [S1] SQLite SQL Language Expressions: IN and NOT IN sqlite.org · Checked
