IN Operator
Introduction
When one column should match any of several values, IN lists them in one test: the customers in SP, RJ or MG, the orders that are processing, invoiced or shipped.
Here you will:
- see how
INchecks a value against a list - compare it with the same test written with
OR - use it with text and with numbers
- leave values out with
NOT IN - take the list from a subquery
- meet the traps, NULL above all
What is the IN Operator?
IN keeps a row when its value equals any value in a list. It answers TRUE when the value is in the list and FALSE when it is not.
Syntax
SELECT columns
FROM table_name
WHERE column IN (value1, value2, value3);
How It Works
- For each row, the database takes the column's value.
- It checks whether the value equals any value in the list.
- The row is kept when one does, and left out when none does.
Basic Example
-- Customers in three states
SELECT customer_id, customer_city, customer_state
FROM olist_customers_dataset
WHERE customer_state IN ('SP', 'RJ', 'MG');66,233 customers: those in SP, RJ or MG, the three states with the most customers.

IN vs Multiple OR Conditions
IN says what a chain of OR on one column says.
Comparison
Using OR:
WHERE customer_state = 'SP'
OR customer_state = 'RJ'
OR customer_state = 'MG'
OR customer_state = 'RS';
Using IN:
WHERE customer_state IN ('SP', 'RJ', 'MG', 'RS');
Both keep 71,699 customers.
Benefits of IN
- Readability: the list is read at a glance
- Easy to change: a value is added or removed in one place
- Fewer mistakes: the column is written once
Speed is not a reason to choose one over the other here: for the three states of the example above, SQLite makes the same plan for IN and for its chain of OR, SEARCH olist_customers_dataset USING INDEX idx_customers_state (customer_state=?), reading each state's customers through the index on customer_state.
When to Use Each
Use IN when one column is checked against several values that you know.
Use OR when the conditions test different columns, or use different comparisons:
-- Different columns
WHERE customer_state = 'SP'
OR customer_city = 'rio de janeiro';
-- Different comparisons
WHERE price > 100
OR freight_value > 50;
The first keeps 48,628 customers, the second 41,012 items.
IN with Text Values
IN is most often used with text values.
State Filtering
-- Customers in the four states of the Southeast
SELECT customer_id, customer_city, customer_state
FROM olist_customers_dataset
WHERE customer_state IN ('SP', 'RJ', 'MG', 'ES');68,266 customers.
Order Status Filtering
-- Orders still on their way
SELECT order_id, order_status, order_purchase_timestamp
FROM olist_orders_dataset
WHERE order_status IN ('processing', 'invoiced', 'shipped');1,722 orders.
Payment Type Filtering
-- Card payments, credit or debit
SELECT order_id, payment_type, payment_value
FROM olist_order_payments_dataset
WHERE payment_type IN ('credit_card', 'debit_card');78,324 payments.
City Filtering
-- Customers in four big cities
SELECT customer_id, customer_city, customer_state
FROM olist_customers_dataset
WHERE customer_city IN ('sao paulo', 'rio de janeiro', 'belo horizonte', 'brasilia');27,326 customers.
Remember: Single Quotes for Text
Text values in the list need single quotes. Without them, SQL reads each word as the name of a column:
-- Without quotes, SQL reads the words as column names
SELECT order_id, order_status
FROM olist_orders_dataset
WHERE order_status IN (delivered, shipped);The site answers no such column: delivered.
IN with Numeric Values
IN works with numbers the same way.
Review Score Filtering
-- Reviews scored 4 or 5
SELECT order_id, review_score
FROM olist_order_reviews_dataset
WHERE review_score IN (4, 5);76,620 reviews.
Payment Installments
-- Payments in 1, 3, 6 or 12 installments
SELECT order_id, payment_type, payment_installments, payment_value
FROM olist_order_payments_dataset
WHERE payment_installments IN (1, 3, 6, 12);67,060 payments.
Item Numbers
-- The second, third and fourth item of each order
SELECT order_id, order_item_id, product_id, price
FROM olist_order_items_dataset
WHERE order_item_id IN (2, 3, 4);13,055 rows. order_item_id numbers an item within its order, 1 for the first, so this keeps the second, third and fourth item rows of their orders, not the orders that hold several items.
No Quotes for Numbers
-- Numbers without quotes
WHERE review_score IN (1, 2, 3)
-- Quoted, the same rows here
WHERE review_score IN ('1', '2', '3')
Both keep 23,380 reviews. review_score holds integers, and the values in a list have no type of their own for the comparison, so SQLite turns '1' into the number 1 before it compares. Its rule: "If one operand has INTEGER, REAL or NUMERIC affinity and the other operand has TEXT or BLOB or no affinity then NUMERIC affinity is applied to the other operand." Write numbers without quotes all the same: it says what they are.
NOT IN
NOT IN keeps the rows whose value is none of the values in the list.
Syntax
WHERE column NOT IN (value1, value2, value3)
Example: Leave Out Three States
-- Customers outside the three states with the most customers
SELECT customer_id, customer_city, customer_state
FROM olist_customers_dataset
WHERE customer_state NOT IN ('SP', 'RJ', 'MG');33,208 customers.
Example: Leave Out Two Statuses
-- Orders that are neither canceled nor unavailable
SELECT order_id, order_status
FROM olist_orders_dataset
WHERE order_status NOT IN ('canceled', 'unavailable');98,207 orders. These are not only the completed orders: every other status stays, processing and shipped among them (delivered, shipped, invoiced, processing, created and approved).
Example: Leave Out Low Scores
-- Reviews scored 3, 4 or 5
SELECT order_id, review_score
FROM olist_order_reviews_dataset
WHERE review_score NOT IN (1, 2);84,907 reviews.
Example: Leave Out Two Payment Types
-- Payments by neither credit card nor boleto
SELECT order_id, payment_type, payment_value
FROM olist_order_payments_dataset
WHERE payment_type NOT IN ('credit_card', 'boleto');7,307 payments, by voucher, debit_card and not_defined.
IN with Subqueries
The list can come from a query, written in brackets in its place: a subquery.
Syntax
WHERE column IN (SELECT column FROM table WHERE condition)
Example: Customers Who Placed Orders
-- Customers who placed an order
SELECT customer_id, customer_city, customer_state
FROM olist_customers_dataset
WHERE customer_id IN (
SELECT customer_id
FROM olist_orders_dataset
);All 99,441 customers. In this dataset each order has its own customer id, so every customer has exactly one order, and none is left out.
Example: Products That Were Sold
-- Products that were sold
SELECT product_id, product_category_name
FROM olist_products_dataset
WHERE product_id IN (
SELECT product_id
FROM olist_order_items_dataset
);All 32,951 products: every product here was sold at least once.
Example: Orders from One City
-- Orders of the customers in the city of sao paulo
SELECT order_id, customer_id, order_status
FROM olist_orders_dataset
WHERE customer_id IN (
SELECT customer_id
FROM olist_customers_dataset
WHERE customer_city = 'sao paulo'
);15,540 orders. SQLite runs the subquery once and keeps its list, then finds each order through the index on customer_id: its plan reads SEARCH olist_orders_dataset USING INDEX idx_orders_customer (customer_id=?), then LIST SUBQUERY 1, then SCAN olist_customers_dataset.
NOT IN with Subqueries
-- Customers who never placed an order
SELECT customer_id, customer_city
FROM olist_customers_dataset
WHERE customer_id NOT IN (
SELECT customer_id
FROM olist_orders_dataset
WHERE customer_id IS NOT NULL
);No row, since every customer has an order. The IS NOT NULL in the subquery is there on purpose, as the first pitfall below shows.
Common Pitfalls
Pitfall 1: NULL in NOT IN
If the list holds a NULL, NOT IN keeps no row at all. A list from a subquery can bring one in without your writing it: here the categories of the products over 10 kg, 38 of which have no category.
-- Products in a category with no product over 10 kg: no row
SELECT COUNT(*) AS products
FROM olist_products_dataset
WHERE product_category_name NOT IN (
SELECT product_category_name
FROM olist_products_dataset
WHERE product_weight_g > 10000
);0 products. For a product in another category, NOT IN asks whether its category differs from every value in the list; against the NULL the answer is NULL, so the whole test is NULL, and the row is left out. PostgreSQL answers the same way. Leave the NULLs out of the list:
-- The same, with the NULLs left out of the list
SELECT COUNT(*) AS products
FROM olist_products_dataset
WHERE product_category_name NOT IN (
SELECT product_category_name
FROM olist_products_dataset
WHERE product_weight_g > 10000
AND product_category_name IS NOT NULL
);2,295 products, in the 26 categories that have no product over 10 kg.
Pitfall 2: An Empty List
SQLite takes an empty list. In its manual's words: "Note that SQLite allows the parenthesized list of scalar values on the right-hand side of an IN or NOT IN operator to be an empty list but most other SQL database engines and the SQL92 standard require the list to contain at least one element."
-- An empty list: no value is in it
SELECT COUNT(*) AS orders
FROM olist_orders_dataset
WHERE order_status IN ();0 orders: no value is in an empty list.
-- An empty list: every value is outside it
SELECT COUNT(*) AS orders
FROM olist_orders_dataset
WHERE order_status NOT IN ();All 99,441 orders: every value is outside it. PostgreSQL refuses the same list with syntax error at or near ")".
Pitfall 3: Very Long Lists
A list of thousands of typed values is hard to read and to keep right. When the values are in a table, take them from it with a subquery, as above.
Pitfall 4: Case
Text compares by the characters' codes, so 'sp' is not 'SP':
-- The states are stored in capitals
SELECT COUNT(*) AS customers
FROM olist_customers_dataset
WHERE customer_state IN ('sp', 'rj');0 customers: the states are stored in capitals. UPPER(customer_state) IN ('SP', 'RJ') would find the 54,598, but with the column inside a function SQLite can no longer use its index: the plan reads SCAN olist_customers_dataset, every row. Write the list in the case the data is stored in.
Try IN on the course's rows. Pick a table and a condition, then test the rows one at a time or all at once: each chip shows its own answer, the row's lamp the answer for the whole condition, and only a TRUE row goes through the gate into the result. The customers show the list beside its chain of OR, NOT IN and the empty list; the products show a missing weight, which is in no list, and a NULL in a NOT IN list.

Practice
Practice using IN in different situations.
Exercise 1: Basic IN
Find customers in the South: RS, SC and PR:
-- Customers in the South: RS, SC and PR
SELECT customer_id, customer_city, customer_state
FROM olist_customers_dataset
WHERE customer_state IN ('RS', 'SC', 'PR')
LIMIT 10;Ten of the 14,148 such customers.
Exercise 2: IN with Order Status
Find orders that are invoiced, shipped or delivered:
-- Orders invoiced, shipped or delivered
SELECT order_id, order_status, order_purchase_timestamp
FROM olist_orders_dataset
WHERE order_status IN ('invoiced', 'shipped', 'delivered')
LIMIT 10;Ten of the 97,899 such orders.
Exercise 3: IN with Numbers
Find reviews scored 4 or 5, with their titles:
-- Reviews scored 4 or 5, with their titles
SELECT order_id, review_score, review_comment_title
FROM olist_order_reviews_dataset
WHERE review_score IN (4, 5)
LIMIT 10;Ten of the 76,620 such reviews.
Exercise 4: NOT IN
Find payments by other means than credit card and boleto:
-- Payments by other means than credit card and boleto
SELECT order_id, payment_type, payment_value
FROM olist_order_payments_dataset
WHERE payment_type NOT IN ('credit_card', 'boleto')
LIMIT 10;Ten of the 7,307 such payments.
Exercise 5: IN with Subquery
Find products that were sold, with a subquery:
-- Products that were sold
SELECT product_id, product_category_name
FROM olist_products_dataset
WHERE product_id IN (
SELECT product_id
FROM olist_order_items_dataset
)
LIMIT 10;Ten of the 32,951: every product was sold.
Summary
Key Takeaways
- IN keeps a row when its value equals any value in the list
- It says what a chain of OR on one column says
- Text values need single quotes; numbers need none
- NOT IN leaves the values in the list out
- A subquery can give the list
- A NULL in the list makes NOT IN keep no row
Quick Reference
-- A list of text values
WHERE column IN ('value1', 'value2', 'value3')
-- A list of numbers
WHERE review_score IN (4, 5)
-- Values left out
WHERE order_status NOT IN ('canceled', 'unavailable')
-- The list from a subquery
WHERE customer_id IN (SELECT customer_id FROM olist_customers_dataset)
-- NOT IN with a subquery, NULLs left out
WHERE column NOT IN (
SELECT column FROM table_name
WHERE column IS NOT NULL
)
IN vs OR vs BETWEEN
| Scenario | Best Operator |
|---|---|
| One column, several values | IN |
| Different columns or comparisons | OR |
| A range of values | BETWEEN |
| Several values left out | NOT IN |
What's Next?
LIKE Operator matches text against a pattern.