IN

The SQL IN Operator

IN lets you specify several values in a WHERE clause. It is shorthand for multiple OR conditions.

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

Example

Customers in India, UK or USA:

SELECT * FROM Customers
WHERE Country IN ('India', 'UK', 'USA');

Result: 5 rows

CustomerIDCustomerNameContactNameCityCountry
1Aarav TradersAarav ShahMumbaiIndia
2Blue Harbor FoodsEmma ClarkLondonUK
4Delta MartPriya NairBengaluruIndia
5Evergreen GrocersNoah WilsonNew YorkUSA
8Harbor LightsOlivia MartinLondonUK

NOT IN

Example

Customers not in those countries:

SELECT * FROM Customers
WHERE Country NOT IN ('India', 'UK', 'USA');

Result: 3 rows

CustomerIDCustomerNameContactNameCityCountry
3Cedar & CoLiam BrownTorontoCanada
6Fjord SuppliesIngrid OlsenOsloNorway
7Golden SpoonMei ChenNULLSingapore

IN (SELECT)

The list can come from a subquery.

Example

Customers who have placed at least one order:

SELECT CustomerName FROM Customers
WHERE CustomerID IN (SELECT CustomerID FROM Orders);

Result: 6 rows

CustomerName
Aarav Traders
Blue Harbor Foods
Cedar & Co
Delta Mart
Evergreen Grocers
Golden Spoon

Note: Be careful with NOT IN and a subquery that can return NULL: if any value in the list is NULL, NOT IN returns no rows. NOT EXISTS avoids this (see the Subqueries module).