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