// conditional reference

SQL IN operator

True when the value matches any member of the list or subquery.

カテゴリ
条件付き
Returns
boolean
In this category
16 of 43

構文

x IN (1, 2, 3) | x IN (SELECT ...)

エンジンサポート

エンジンSupportメモ
SQL Server はい Supported under this name.
PostgreSQL はい Supported under this name.
MySQL はい Supported under this name.
SQLite はい Supported under this name.
Oracle はい Supported under this name.

例

Any engine

SELECT id, status FROM orders WHERE status IN ('new', 'paid');

PostgreSQL

SELECT * FROM orders WHERE customer_id IN (SELECT id FROM vip_customers);

知っておくと便利なこと

  • x IN ('a', NULL) matches 'a' rows only; the NULL member makes the remaining comparisons UNKNOWN, never true.
  • NOT IN with a single NULL in the subquery returns zero rows: the most famous NULL trap in SQL.
  • For subqueries, IN is a semi-join; correlated cases often read and plan better as EXISTS.
  • Long literal lists hit engine limits: 1000 expressions in Oracle, 2100 parameters in SQL Server.

Questions about IN

Why does IN ignore NULL members?

x IN (a, NULL) is x = a OR x = NULL; if the first is false the second is UNKNOWN, and WHERE keeps only TRUE.

IN vs EXISTS for subqueries?

For uncorrelated subqueries the planner usually makes them identical; for correlated checks, EXISTS states the intent and short-circuits.

How big can an IN list be?

Oracle caps at 1000 expressions (ORA-01795); SQL Server at 2100 parameters; prefer a temp table or = ANY(array) at that size.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.