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
- LIKE Patterns and Wildcards
- IN and NOT IN for Sets
- BETWEEN for Ranges
- IS NULL, IS NOT NULL and COALESCE