// conditional reference

SQL IN list limits

The per-engine ceilings on IN lists and the standard ways around them.

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

構文

x IN (SELECT id FROM temp_keys) | x = ANY (ARRAY[...])

エンジンサポート

エンジンSupportメモ
SQL Server Different name or syntax 2100 parameter limit per request
PostgreSQL はい no hard limit, but giant lists plan and cache badly
MySQL はい Supported under this name.
SQLite Different name or syntax SQLITE_MAX_VARIABLE_NUMBER: default 999 before 3.32, then 32766
Oracle Different name or syntax ORA-01795: maximum 1000 expressions in a list

例

PostgreSQL

SELECT * FROM t WHERE id = ANY($1::bigint[]);

SQL Server

CREATE TABLE #keys (id INT PRIMARY KEY); INSERT INTO #keys VALUES (1), (2); SELECT * FROM orders o JOIN #keys k ON o.id = k.id;

知っておくと便利なこと

  • Oracle counts expressions: 1000 literals max. Because the list is usually generated, ORA-01795 tends to surface only when production data first pushes it past 1000.
  • SQL Server's 2100 limit counts every parameterized value in the request, so other parameters eat into it.
  • SQLite's cap is on bound variables; it jumped from 999 to a 32766 default in 3.32.
  • The durable fix is a temp table plus IN (subquery) or a join; on PostgreSQL, x = ANY($1::bigint[]) squeezes the list into one parameter.

Questions about IN

Why does Oracle fail with ORA-01795?

More than 1000 expressions in one IN list; split the list or load the keys into a table.

What is the SQLite limit on IN?

Bound variables: 999 before 3.32, 32766 by default after, overridable at compile time.

Best fix for huge ID lists?

A temp table with a primary key and a join: the planner gets statistics instead of a wall of literals.

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.