Description

The SQL keyword IN is used within a WHERE condition to compare a column value against a set of values.

It is a shorthand for multiple OR conditions.

SELECT With IN

The below SQL fetches all the customers with customer IDs 1, 2, 3, 4, and 5.

SELECT *
FROM customers
WHERE customer_id IN (1,2,3,4,5);

The below SQL fetches all the customers from countries "Spain", "Germany" and "France".

SELECT *
FROM customers
WHERE country IN ("Spain", "Germany", "France");

SELECT With NOT IN

The below SQL fetches all the customers, not having customer IDs 1, 2, 3, 4, and 5.

SELECT *
FROM customers
WHERE customer_id NOT IN (1,2,3,4,5);

The below SQL fetches all the customers, not from countries "Spain", "Germany" and "France".

SELECT *
FROM customers
WHERE country NOT IN ("Spain", "Germany", "France");

SELECT Returns Values for IN

The below SQL fetches all the customers from countries, to which suppliers are available.

SELECT * FROM customers
WHERE country IN (SELECT country FROM suppliers);

Related Links