// 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.