Black friday

Save big!

All courses under $5 - for a limited time!

Code has been added to clipboard!

How to Use the SQL IN Operator

Reading time 2 min
Published Aug 10, 2017
Updated Oct 3, 2019

SQL IN Operator Introduced

In SQL, IN is a logical operator that allows you to filter a set of values against a specific condition. It is usually used with the WHERE clause.

In the parentheses, you can define the exact values you need to be matched by the data entries:

Example
SELECT column_demo(z)
FROM table_demo
WHERE column_demo IN (value1, value2, ...);

You can also use the SELECT statement to create a subquery that returns a list of values in a specified column:

Example
SELECT column_demo(z)
FROM table_demo
WHERE column_demo IN (SELECT STATEMENT);

Demo Databases

In this tutorial, we will be using two demo tables for our data – Developers and Customers.

Developers

ID Name City Country
1 Tom Kurkutis New York USA
2 Ana Fernandez London UK
3 Antonio Indigo Paris France
4 Aarav Kaelin Delhi India
5 Andrew Tumota Miami USA
6 Basma Zlata Miami USA
Theory is great, but we recommend digging deeper!

Customers

id name contact address city postal_code country
1 Ben Choplinks Ben Choplink Obeesre Str. 51 Rome 11207 Italy
2 Donald Rich Donald Richario Avda. de la Confgfstitución 4122 Tallin 17021 Estonia
3 Lilly Smilkins Lilly Smilkin Matadsderos 2312 Eguero 14023 Mexico
4 Brandinina Tom Hitchins 110 Hanegover Sq. London WB2 2DP UK
5 Carizmos Christiano Kerrys Berguvsesvägen 9 Lulea S-968 43 Sweden

Examples of Using SQL IN()

The example below picks all the developers from USA, France and India:

Example
SELECT * FROM Developers
WHERE Country IN ('USA', 'France', 'India');

For an opposite result, you can also use the NOT IN operator in SQL. The code in this next example picks out only the developers who are not from those countries:

Example
SELECT * FROM Developers
WHERE Country NOT IN ('USA', 'France', 'India');

Working with IN() in Multiple Tables

In the last example, we will try something a bit more complicated. The code you can see below uses both of our demo tables to pick all the developers whose countries match those of the customers:

Example
SELECT * FROM Developers
WHERE Country IN (SELECT Country FROM Customers);

Instead of specifying the values for SQL IN(), we include SELECT Country FROM Customers within the parentheses. This means the SELECT statement will take the values from the Country column in the Customers table.