How to Fix ORA-01795: Maximum Expressions in a List

Fix Oracle error ORA-01795 when your SQL WHERE IN clause exceeds 1,000 items. Learn batching, tuple workarounds, and temporary tables.

Oracle database throws error ORA-01795: maximum number of expressions in a list is 1000 whenever a single expression list, such as the value list of an IN condition, contains more than 1,000 expressions. To resolve this error immediately, you must either split your values across multiple OR blocks, use a composite tuple workaround, or load the values into a temporary table and join against it.

Why does Oracle enforce the 1,000-item limit?

Oracle documents the limit in the SQL Language Reference: a comma-delimited expression list can contain at most 1,000 expressions. The official error page for ORA-01795 gives one action: reduce the number of expressions in the list. Oracle does not publish the internal reason for the cap, and there is no setting that raises it. Very long lists are expensive anyway, because each distinct list produces a new statement that must be hard-parsed and optimized.

The limit counts expressions, not only literals: bind variables such as :1, :2, ... count in the same way. It applies to explicit lists of values. Subqueries such as WHERE id IN (SELECT id FROM staging_table) do not trigger ORA-01795, regardless of whether the subquery returns ten rows or ten million rows, because a subquery is a row source, not an explicit expression list.

Comparison of IN list limits across major relational databases

Different database engines handle large IN clauses with varying restrictions, memory penalties, and error codes:

Database Engine Maximum Literal Items in IN List Error Code / Failure Mode Recommended Approach for Large Lists
Oracle Database 1,000 items per list ORA-01795: maximum expressions exceeded Global Temporary Table or chunked OR
Microsoft SQL Server Approximately 2,100 parameters Msg 8623 / RPC parameter limit reached Table-Valued Parameters (TVP) or temp table
PostgreSQL Limited by memory / query length Slow planning phase; memory spike Array unnesting: = ANY(ARRAY[...]::int[])
MySQL / MariaDB Limited by max_allowed_packet Packet larger than max_allowed_packet Temporary staging table or batching

Four battle-tested workarounds for ORA-01795

Select the workaround that matches your architecture, application permissions, and security constraints:

1. The chunked OR workaround

The simplest application-level fix involves slicing your dataset into chunks of 999 or 1,000 items and chaining them together using the OR operator.

SELECT employee_id, first_name, last_name, department_id
FROM employees
WHERE employee_id IN (1, 2, 3, /* ... up to 999 items */)
   OR employee_id IN (1000, 1001, 1002, /* ... up to 1999 items */)
   OR employee_id IN (2000, 2001, 2002);

While functional, this approach generates massive SQL text that pollutes the Oracle Shared Pool if literal values change with every execution, increasing parse times.

2. The composite tuple workaround

Oracle evaluates multi-column tuples differently than single expressions. By wrapping the column in a dummy two-part tuple, you bypass the single-column expression counter in older and modern Oracle versions alike.

SELECT employee_id, first_name, last_name, department_id
FROM employees
WHERE (1, employee_id) IN (
    (1, 101),
    (1, 102),
    (1, 103),
    /* ... can safely exceed 1,000 items */
    (1, 2450)
);

3. Global Temporary Tables (Recommended for high volume)

For sets exceeding 5,000 items, pushing values into an indexed Global Temporary Table (GTT) provides optimal execution plans and prevents cursor cache pollution.

-- Step 1: Create session-specific staging table (one-time setup)
CREATE GLOBAL TEMPORARY TABLE gtt_filter_ids (
    target_id NUMBER PRIMARY KEY
) ON COMMIT PRESERVE ROWS;

-- Step 2: Batch insert IDs, then run an optimized hash join
INSERT INTO gtt_filter_ids (target_id) VALUES (101);
-- ... insert remaining IDs

SELECT e.employee_id, e.first_name, e.last_name, e.department_id
FROM employees e
INNER JOIN gtt_filter_ids g ON e.employee_id = g.target_id;

4. Cleaning input data before formatting queries

When extracting raw IDs from spreadsheets or user forms, duplicate entries frequently inflate your list beyond the 1,000 threshold unnecessarily.

Before constructing your SQL statement:

  • Run your list through a browser-based duplicate remover to strip redundant IDs, eliminate trailing whitespace, and sort elements.
  • Format the clean column directly into SQL syntax using a dedicated SQL formatter, which wraps string values in single quotes and adds commas without uploading proprietary data to external cloud servers.

Production Pro-Tip: The hidden optimizer penalty of giant IN lists

Even when workarounds succeed, passing thousands of items inside an IN list forces the Oracle cost-based optimizer (CBO) to generate complex execution plans.

If the target column has a B-Tree index, Oracle attempts to evaluate range scans across every individual value. Once the estimated cost of these multiple index lookups exceeds a full table scan, the optimizer abandons the index entirely, degrading query latency. If you frequently query against more than 2,000 items, migrate to temporary tables or parameterized array binds.

Frequently Asked Questions (FAQ)

Does ORA-01795 apply to subqueries?

No. Queries formatted as WHERE column_name IN (SELECT id FROM table_name) do not count toward the 1,000-item expression limit. Oracle evaluates subqueries as joins or semi-joins rather than discrete literal values.

Can I increase the 1,000 limit in Oracle database settings?

No. The 1,000-item limit is hardcoded into the Oracle SQL parser engine. There are no initialization parameters (init.ora / spfile) or hidden database configuration flags that permit increasing this threshold.

Why does the tuple workaround work in Oracle?

The Oracle SQL Reference describes a list of expression sets differently from a single list: each set is limited to 1,000 expressions, but the number of sets is not limited in the same way. A tuple list such as (col1, 0) IN ((val1, 0), (val2, 0), ...) is a list of two-element sets, so it is not rejected after 1,000 items. Test this workaround on your Oracle version and watch its parse time, because it is a side effect of the grammar rather than a feature intended for bulk filtering.

References & Official Sources