Module 02 · lesson 5

IN: a list of values

Marketing needs products from a few specific categories — "Books" and "Clothing" — for a themed selection. Writing OR for each category is tiring.

A list instead of a pile of ORs

When you need to check whether a value matches one of several, instead of a long chain of OR it's handier to use IN with a list:

SELECT name, category 
FROM products
WHERE category IN ('Electronics', 'Home');

This is a shorter, more readable form of category = 'Electronics' OR category = 'Home'. The negation is NOT IN (the value is not in the list).

Careful: if a NULL turns up in a NOT IN list, the result can become empty (we'll cover the subtleties of NULL later).

Your turn

You need products from the "Books" and "Clothing" categories. Output their name and category.

Summary

  • IN checks membership in a list — a compact replacement for many ORs.
  • NOT IN — the opposite of IN (not in the list).