Skip to content
/ Chapter 3 · Filtering in Depth
Lesson 3.3 · garden_shop
Lesson 3.3

SQL IN vs OR: IN and NOT IN Operators

Lists of allowed or excluded values are clearer with IN than repeated OR checks.

When one column can match several values, IN keeps the query shorter and easier to scan. It asks whether a value appears in a list.

NOT IN excludes values found in the list, but it isn't a perfect logical opposite when nulls are involved. The null trap is covered below.

SQL IN vs OR

IN and repeated OR checks can mean the same thing when every comparison is against the same column. Use IN for that case because it is shorter and makes the list of allowed values obvious.

Same result, easier to scan
-- Repeated OR checks
where state = 'PA'
   or state = 'OR'
   or state = 'FL'

-- Equivalent IN list
where state in ('PA', 'OR', 'FL')
Pattern Best when Example
IN One column can match several known values. state in ('PA', 'OR', 'FL')
OR Different columns or different kinds of conditions can match. price > 20 or quantity_on_hand < 10
Parentheses You mix OR with AND. (state = 'PA' or state = 'OR') and is_active

Keep the list short and explicit

IN is best for a small, known set of values such as states, statuses, or categories. If the list is coming from another table, a join or subquery is usually clearer than copying many values into the SQL by hand.

Pattern
select column_one, column_two
from table_name
where column_name in ('value one', 'value two')
  and other_column not in ('value three')
Ready to run it?

Open the DuckDB playground with the matching dataset and query already filled in.

Common mistakes

Schema · Garden Shop Table · suppliers 4 columns · 6 rows
Table · suppliers

One row per supplier. Some suppliers have no contact email.

4 columns · 6 rows
supplier_id int supplier_name text contact_email text state text
Your task

Show the supplier name and state for suppliers in PA, OR, or FL, leaving out Ironwood Tools, which the shop no longer buys from.

·

Run your query to inspect the result.

Expected answer
  • Columns: supplier_name, state.
  • Rows: 3 suppliers.
  • RootWorks Supply stays in; Ironwood Tools, the other PA supplier, is excluded.