MySQL IN Clause

Introduction to MySQL IN Clause

This MySQL tutorial explains how to use the MySQL IN Clause with syntax and examples. The IN operator allows you to specify multiple values in a WHERE clause.

IN Syntax

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

To understand MySQL IN Clause, consider an “customers” table, which is having the following records:

SELECT * FROM customers;

IN Operator Examples

Examples 1:

Now suppose based on the above table you want to selects all customers that are located in “London”, “Berlin” and “México D.F” city:

SELECT * FROM customers
WHERE City IN ('London', 'Berlin', 'México D.F');

Examples 2:

In other example the following MySQL statement selects all customers that are NOT located in “London”, “Berlin” and “México D.F” city:

SELECT * FROM customers
WHERE City NOT IN ('London', 'Berlin', 'México D.F');

Examples 3:

Below is a selection from the “orders” table, which is having the following records:

SELECT * FROM orders;

The following MySQL statement selects all customers who have made order:

SELECT * FROM customers
WHERE CustomerID IN (SELECT CustomerID FROM orders);

Leave A Reply

Your email address will not be published.