0PricingLogin
SQL Academy · Lesson

IN and NOT IN for Sets

Filter against value lists with IN and NOT IN, learn the NULL gotcha in NOT IN, and convert to EXISTS for safe semantics.

IN: Match Against a List

IN is a shortcut for many ORs:

SELECT * FROM users WHERE country IN ('US', 'CA', 'GB');

-- Equivalent to:
SELECT * FROM users
WHERE country = 'US' OR country = 'CA' OR country = 'GB';

IN with a Subquery

You can use a subquery on the right side:

SELECT * FROM orders
WHERE user_id IN (
  SELECT id FROM users WHERE country = 'US'
);

All lessons in this course

  1. LIKE Patterns and Wildcards
  2. IN and NOT IN for Sets
  3. BETWEEN for Ranges
  4. IS NULL, IS NOT NULL and COALESCE
← Back to SQL Academy