bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/SQL/SQL Tutorial
SQL•SQL Tutorial

SQL IN Operator

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind SQL IN Operator?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ * FROM Customers
3Order

Put the learning moves in the order that makes the concept easiest to apply.

The IN operator is used in the WHERE clause to check if a specified column's value matches any value within a provided list.
The NOT IN Operator
The SQL IN Operator

The SQL IN Operator

The IN operator is used in the WHERE clause to check if a specified column's value matches any value within a provided list.

The IN operator functions as a shorthand for multiple OR conditions, making queries shorter and more readable.

The following SQL uses the IN operator to select all customers from Germany, France, or UK:

Example

SELECT * FROM Customers

 WHERE Country IN ('Germany', 'France', 'UK');

The following SQL uses multiple OR conditions to select all customers from Germany, France, or UK (same result, but longer code):

Example

SELECT * FROM Customers
WHERE Country = 'Germany' OR Country = 'France' OR Country = 'UK';

Syntax

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

Demo Database

Below is a selection from the Customers table used in the examples:

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.05021Mexico
3Antonio Moreno TaqueríaAntonio MorenoMataderos 2312México D.F.05023Mexico
4Around the HornThomas Hardy120 Hanover Sq.LondonWA1 1DPUK
5Berglunds snabbköpChristina BerglundBerguvsvägen 8LuleåS-958 22Sweden

The NOT IN Operator

By using the NOT IN operator, you return all records that are NOT any of the values in the list.

Example

SELECT * FROM Customers

 WHERE Country NOT IN ('Germany', 'France', 'UK');

In (select)

You can also use IN with a subquery in the WHERE clause.

With a subquery you can return all records from the main query that are present in the result of the subquery.

The following SQL returns all customers who also have an order in the "Orders" table:

Example

SELECT * FROM Customers

WHERE CustomerID IN (SELECT CustomerID FROM Orders);

Not In (select)

The result in the example above returned 74 records, that means that there are 17 customers that haven't placed any orders.

Let us check if that is correct, by using the NOT IN operator.

The following SQL returns all customers who do NOT have any orders in the "Orders" table:

Example

SELECT * FROM Customers

WHERE CustomerID NOT IN (SELECT CustomerID FROM Orders);

Previous

SQL Wildcards

Next

SQL BETWEEN Operator