SQL IN
IN checks whether a value matches any value in a list. It is a shorter, clearer way to write several OR conditions on the same column.
IN also accepts a subquery, which makes it a simple way to filter one table by values found in another.
Syntax
WHERE column IN (value1, value2, ...) WHERE column IN (SELECT ...)
Try it yourself
Run the example below on a real PostgreSQL database, then change it and run it again. Nothing to install, no sign-up.
Examples
1. IN with a list
SELECT first_name, country FROM customers WHERE country IN ('Japan', 'France', 'Canada');Matches any of the three countries.
2. NOT IN
SELECT id, status FROM orders WHERE status NOT IN ('delivered', 'shipped');Everything except the listed values.
3. IN with a subquery
SELECT name FROM products WHERE id IN (SELECT product_id FROM order_items WHERE quantity = 3);
Products that were bought three at a time.
Common mistakes
- Using NOT IN with a subquery that can return NULL. If any value is NULL, NOT IN returns no rows at all. Use NOT EXISTS instead.
Practice: test yourself
This challenge is from our interactive course. Write the query and press Check answer.
Return first_name and country of customers from Japan, France or Canada.
FAQ
Is IN faster than OR?
In PostgreSQL they are usually planned the same way. Choose IN because it is easier to read.
Learn SQL step by step
65 hands-on exercises, streaks, a leaderboard and a certificate.