TExcelTAD
Home › SQL › SQL IN

SQL IN

The IN operator checks whether a value matches any value in a list. It is a compact replacement for many OR conditions on the same column.

sql
SELECT columns
FROM table_name
WHERE column IN (value1, value2, ...);

A list of values

sql · try it yourself
SELECT name, city, country
FROM customers
WHERE country IN ('Germany', 'Austria', 'Norway', 'Sweden')
ORDER BY country, name;
name           | city      | country
---------------+-----------+--------
Nora Fischer   | Vienna    | Austria
Corner Cafe    | Berlin    | Germany
Priya Kowalski | Berlin    | Germany
Arjun Sharma   | Oslo      | Norway
Hugo Berg      | Stockholm | Sweden
(5 rows)

This is the same as country = 'Germany' OR country = 'Austria' OR …, but shorter and easier to extend.

NOT IN

sql · try it yourself
SELECT status, COUNT(*) AS orders
FROM orders
WHERE status NOT IN ('delivered', 'cancelled')
GROUP BY status
ORDER BY status;
status     | orders
-----------+-------
processing | 8
shipped    | 9
(2 rows)

IN with a subquery

The list can come from another query. Which products were ever ordered by customer 17 (Amara Costa)?

sql · try it yourself
SELECT product_id, name
FROM products
WHERE product_id IN (
  SELECT oi.product_id
  FROM order_items AS oi
  JOIN orders AS o ON o.order_id = oi.order_id
  WHERE o.customer_id = 17
)
ORDER BY product_id;
product_id | name
-----------+----------------------------
3          | Orange Juice
4          | Sparkling Water
10         | Whole Milk
15         | Cherry Tomatoes
19         | Tortilla Chips
20         | USB-C Cable
21         | Wireless Mouse
23         | Noise-Cancelling Headphones
25         | Chef Knife
26         | Cast Iron Pan
(10 rows)

Duplicates in the subquery don't matter — IN only asks "is it in there at all?".

Customers who have never ordered:

sql · try it yourself
SELECT customer_id, name
FROM customers
WHERE customer_id NOT IN (SELECT customer_id FROM orders)
ORDER BY customer_id;
customer_id | name
------------+----------------
4           | Pixel Studio
8           | Night Owl Books
10          | Daily Bread
13          | Priya Kowalski
19          | Nora Fischer
23          | Zara Ali
24          | Hugo Berg
(7 rows)

The NOT IN and NULL trap

If the list contains a NULL, NOT IN returns no rows at all: x NOT IN (1, NULL) means x <> 1 AND x <> NULL, and x <> NULL is unknown.

sql · try it yourself
SELECT COUNT(*) AS without_null FROM products WHERE product_id NOT IN (1, 2);

SELECT COUNT(*) AS with_null FROM products WHERE product_id NOT IN (1, 2, NULL);
without_null
------------
28
(1 row)

with_null
---------
0
(1 row)

Tip: When a NOT IN subquery could return NULLs, add WHERE col IS NOT NULL inside it — or use NOT EXISTS, which does not have this problem.

Summary

Form Same as
x IN (a, b) x = a OR x = b
x NOT IN (a, b) x <> a AND x <> b
x IN (SELECT …) EXISTS (SELECT … WHERE … = x)

Test yourself

Which condition equals status = 'shipped' OR status = 'delivered'?

  1. status IN ('shipped', 'delivered')
  2. status BETWEEN 'shipped' AND 'delivered'
  3. status = ('shipped', 'delivered')
  4. status LIKE 'shipped|delivered'

What does WHERE id NOT IN (1, NULL) return?

  1. All rows except id 1
  2. All rows
  3. No rows
  4. Only the NULL row