IN Operator
IN is a concise alternative to multiple OR comparisons on the same column.
Core Concepts
flowchart LR
A[Column value] --> B{Is value in list?}
B -->|Yes| C[Include row]
B -->|No| D[Exclude row]
| Style | Example |
|---|---|
| Multiple ORs | status='new' OR status='paid' |
| IN list | status IN ('new','paid') |
Code Examples
-- Reuse orders_cmp table from earlier concept.
SELECT order_id, total, status
FROM orders_cmp
WHERE status IN ('new', 'paid')
ORDER BY order_id;
| order_id | total | status |
|---|---|---|
| 1 | 19.99 | new |
| 2 | 120.00 | paid |
| 3 | 75.50 | new |
-- Equivalent logic with OR (longer form).
SELECT order_id, total, status
FROM orders_cmp
WHERE status = 'new' OR status = 'paid'
ORDER BY order_id;
| order_id | total | status |
|---|---|---|
| 1 | 19.99 | new |
| 2 | 120.00 | paid |
| 3 | 75.50 | new |
SQLite-Specific Nuances
SQLite Nuance
IN supports literal lists and subqueries in SQLite.
Common Pitfalls / Best Practices
Pitfall
Using long repeated OR chains when IN would be clearer and less error-prone.
Best Practice
Use IN whenever you compare one column against multiple specific values.
Quick Challenge
Return rows from orders_cmp where status is either new or pending.
View Solution
SELECT order_id, total, status
FROM orders_cmp
WHERE status IN ('new', 'pending');
| order_id | total | status |
|---|---|---|
| 1 | 19.99 | new |
| 3 | 75.50 | new |