Skip to content
/Chapter 3 · Select, Filter, and Sort
Lesson 3.4·garden_shop
Lesson 3.4

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 does the opposite: it removes rows whose value appears in the list.

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')
PatternBest whenExample
INOne column can match several known values.state in ('PA', 'OR', 'FL')
ORDifferent columns or different kinds of conditions can match.price > 20 or quantity_on_hand < 10
ParenthesesYou 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 column_name 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 ShopTable · suppliers4 columns · 6 rows
Table · suppliers

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

4 columns · 6 rows
supplier_id intsupplier_name textcontact_email textstate text
Your task

Show the supplier name and state for suppliers in PA, OR, or FL, but exclude suppliers in PA.

SQL Workbench
query.sqlgarden_shop · SQL engine loading
⌘↵ to run
·
Expected answer
  • Columns: supplier_name, state.
  • Rows: 2 suppliers.
  • States shown: FL and OR.